/    Sign up×
Community /Pin to ProfileBookmark

Radio Button submit and preserve state

Hi…

So, I’ve been messing around with a lot of PHP server side style switchers and while I can get them to work, I have noticed that they lack two things which I hope someone can assist me with.

On is that I want the ability to use radio buttons that do not require the user to click a button to submit. That is, to select the style and have an auto submit. For example, this site has a great one but the user much click a button: [url]http://www.contrastsweb.com/switcher/v2/example.php[/url].

The second is that once the user selects the radio button, as they navigate from page to page, the radio button displays the style in which they have chosen. Again, if they select style #2, as they go from page to page, the radio button is selected for style #2. I don’t know how this could be done except for writing to a cookie or something. Anyway, anyone experience anything similar and could offer some assistance?

Thanks!!!

Tim

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@aaronbdavisJan 16.2006 — I imagine you could do something similar to ASP.Net _doPostBack feature. I am sure exactly how it works but it is done using server generated Javascript. I think the basic Idea uses a hidden input, then emulates posting to the page. If you can get the postback part to work, then you simply test for the value of the hidden input variable. I would suggest using either $_SESSION[] (Which is basically specialized cookies) for transferring the value from one page to the next, and cookies if you want them to be able to remember settings. I'll look around a bit to see if I can figure out the postback part and post here if I find anything.
Copy linkTweet thisAlerts:
@aaronbdavisJan 16.2006 — Well, I am (edit) [I]not[/I] sure how to do it with radio buttons but it is probably fairly easy to change. I did get it to work with a dropdown box here: [URL=http://www.thebaseballwanderer.com/testing/doPostBack.php]here[/URL]
Copy linkTweet thisAlerts:
@web_dudeJan 16.2006 — first, if you want to call form submit once the user changed the radio button, create a function, call it 'mySubmit()' and add to the radio button onchange="mySubmit()"

onsubmit should call form.submit()

The second one, how to transfer info between pages, save the info in the session. this way it will be available during the time the user is in your site.
Copy linkTweet thisAlerts:
@aaronbdavisJan 16.2006 — do you have to set onChange for each radion button in the group or just the first? i.e. [code=html]<form>
<input type="radio" name="foo" value="1" onChange="mySubmit()" />
<input type="radio" name="foo" value="2" onChange="mySubmit()" />
<input type="radio" name="foo" value="3" onChange="mySubmit()" />
<input type="radio" name="foo" value="4" onChange="mySubmit()" />
</form>[/code]
vs.[code=html]<form>
<input type="radio" name="foo" value="1" onChange="mySubmit()" />
<input type="radio" name="foo" value="2" />
<input type="radio" name="foo" value="3" />
<input type="radio" name="foo" value="4" />
</form>[/code]
Copy linkTweet thisAlerts:
@felgallJan 16.2006 — If you define each radio button as follows then the appropriate one will be automatically checked based on the value passed to the page from the previous one.

&lt;input type="radio" name="foo" value="1" &lt;? if ($foo == '1') echo 'checked="checked"' ?&gt; /&gt;
Copy linkTweet thisAlerts:
@RedWingsSuxauthorJan 16.2006 — Thanks, I will get to work and see if I can make it all happen. Thanks for everything!

Tim
Copy linkTweet thisAlerts:
@bokehJan 17.2006 — Having a radio button without a submit button means using javascript. If you are going to use javascript you might as well use that to just import a different stylesheet (which you preloaded). That way there will be no page reload and the style change will be instanteneous (like a real user interface).
Copy linkTweet thisAlerts:
@peteybJan 17.2006 — Could "aaronbdavis" please post the code to the "here" link?
Copy linkTweet thisAlerts:
@aaronbdavisJan 17.2006 — [code=php]
<?php
$choices = array("Zero","One","Two", "Three", "Four", "Five");
$choice = $_POST['bob'];
print "You chose : $choice";
?>
<form method="post" name="foo" id="foo" >
<label>
<select name="bob" id="bob" onChange="this.form.submit();">
<?php
foreach ($choices as $key => $value) {
if ($key == $choice) {
print "ttt<option value="$key" selected="selected" >$value</option>n";
} else {
print "ttt<option value="$key" >$value</option>n";
}
}
?>
</select>
</label>
</form>[/code]
Copy linkTweet thisAlerts:
@HuevoosJan 17.2006 — Having a radio button without a submit button means using javascript. If you are going to use javascript you might as well use that to just import a different stylesheet (which you preloaded). That way there will be no page reload and the style change will be instanteneous (like a real user interface).[/QUOTE]

That's a good idea, and you could then set a cookie with the selected style so they don't loose their selection when navigating away.
Copy linkTweet thisAlerts:
@peteybJan 18.2006 — Just thought everyone should know:

[code=html]
<select name="project">
<option value="a" <? if(isset($_SESSION['project']) && $_SESSION['project'] == 'a') { ?>selected="selected"<? } ?>>a</option>
<option value="b" <? if(isset($_SESSION['project']) && $_SESSION['project'] == 'b') { ?>selected="selected"<? } ?>>b</option>
</select>
[/code]
Copy linkTweet thisAlerts:
@bokehJan 18.2006 — [URL=http://bokehman.com/swap_stylesheet/]Here's how I would do it.[/URL] The only problem with a radio button is it is smaller than a text link hence harder to click on.
×

Success!

Help @RedWingsSux spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...