Click to See Complete Forum and Search --> : Radio Button submit and preserve state


RedWingsSux
01-16-2006, 11:49 AM
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: http://www.contrastsweb.com/switcher/v2/example.php.

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

aaronbdavis
01-16-2006, 12:38 PM
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.

aaronbdavis
01-16-2006, 01:12 PM
Well, I am (edit) not 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: here (http://www.thebaseballwanderer.com/testing/doPostBack.php)

web_dude
01-16-2006, 01:55 PM
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.

aaronbdavis
01-16-2006, 02:32 PM
do you have to set onChange for each radion button in the group or just the first? i.e. <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> vs.<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>

felgall
01-16-2006, 03:30 PM
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.

<input type="radio" name="foo" value="1" <? if ($foo == '1') echo 'checked="checked"' ?> />

RedWingsSux
01-16-2006, 05:52 PM
Thanks, I will get to work and see if I can make it all happen. Thanks for everything!

Tim

bokeh
01-17-2006, 07:40 AM
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).

peteyb
01-17-2006, 08:41 AM
Could "aaronbdavis" please post the code to the "here" link?

aaronbdavis
01-17-2006, 12:14 PM
<?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 "\t\t\t<option value=\"$key\" selected=\"selected\" >$value</option>\n";
} else {
print "\t\t\t<option value=\"$key\" >$value</option>\n";
}
}
?>
</select>
</label>
</form>

Huevoos
01-17-2006, 01:04 PM
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).

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.

peteyb
01-18-2006, 04:49 AM
Just thought everyone should know:


<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>

bokeh
01-18-2006, 10:10 AM
Here's how I would do it. (http://bokehman.com/swap_stylesheet/) The only problem with a radio button is it is smaller than a text link hence harder to click on.