Forum Moderators: coopster

Message Too Old, No Replies

Remembering radio button selection

         

adammc

6:25 am on Mar 6, 2007 (gmt 0)

10+ Year Member



Hello again :)

Just got a quick question... How do I get my form to remember the radio button that was selected before pressing submit (POST)

[php]
<input type="radio" name="drink" id="drink1" value="Yes" ><label for="drink1">Yes</label>
<input type="radio" name="drink" id="drink2" value="No" ><label for="drink2">No</label>
<input type="radio" name="drink" id="drink3" value="Occasionally" ><label for="drink3">Occasionally</label>
[/php]

I am currently using something like this for dropdown lists:
[php]
<?php
if(isset($_POST['family_father'])) {
echo "<option value=\"$_POST[family_father]\">$_POST[family_father]</option>";}
else {
echo "<option value=\"\">please select</option>";
}?>
[/php]

grandpa

7:00 am on Mar 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I use a cookie. Setting a session value would also work. My cookie gets set in the script that I posted to, and read in the original script. I'm not doing this with radio buttons, but with drop down selections. I can't see where that makes a difference...

adammc

7:07 am on Mar 6, 2007 (gmt 0)

10+ Year Member



Hi grandpa,

With my limited PHP knowledge I can only think of doing it like this:

if(isset($_SESSION['drink']) == '')

but this would mean that you would have to check for each of the 3 options then echo the radio button code with one of them marked as checked.

See,s VERY longwinded.

Little help please?

omoutop

7:30 am on Mar 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Unless you pick your radio options from a database, this is the only way to have your form "remember" your choise.

If you can use a database/textfile/etc, then your work will be much easier.

grandpa

7:45 am on Mar 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



if(isset($_SESSION['drink']) == '')

Instead of checking for specific button values, you'll be looking to see if a cookie is set, and if it is, use the value stored there.

------

OK, but this might not be pretty ;) Here's what happens on my page.

The form submits to the same page, basically changing the page view. I make use of Javascript to actually set the cookie.

So you're on my page, and you see the options on the page, and you pick this one.

<form name="ShowCat" action="http://www.example.com/page.php">
<input type="hidden" name="Type5" value="MyValue">
<a rel="nofollow" class="catlist" onmouseover="style.backgroundColor='#FFFFFF';" onmouseout="style.backgroundColor='#CFD2AC'" onMousedown="SelectCatType5()">MyValue</a>
</form>

If you look closely, you'll see the OnMousedown line in my form. When you click that option, the Javascript is fired.

In the HEAD section of the document, you'll find this JS function.

<script language="JavaScript" type="text/javascript">
<!--
function SelectCatType5() {
var a;
a = document.ShowCat.Type5.value;
document.cookie = 'MyValue=' + a + '; expires=Thu, 2 Aug 2007 20:47:11 UTC; path=/'
window.location = "http://www.example.com/page.php"
}
//-->
</script>

The JS sets the cookie, and then it recalls the page.

When the page reloads, it will encounter this bit of php code in the body of the document.

// check to see if the cookie is set
// if not, default to all, otherwise use it
if (isset($HTTP_COOKIE_VARS["MyValue"])) {
$select = $HTTP_COOKIE_VARS["MyValue"];
} else {
$select = 'All';
}

Now I have the value that was selected in the php variable $select. From there, you I can use it any way I want in my php code.

Like I said, not real pretty. Maybe not even applicable to your situation. I seem to want to re-invent the wheel every time I write a new script.

In a nutshell, I grab the value and set a cookie with Javascript, and process the cookie with PHP.

If I was submitting to a different script from this page, I might forego the JS altogether and use PHP for everything. In the page receiving the POST values you can extract the selected option and set your cookie. Then in the original page see if that value has been set. I'm guessing that you do want to value returned to the original page? I'm looking in my source for a better example of this scenario...

adammc

11:13 am on Mar 6, 2007 (gmt 0)

10+ Year Member



thanks for the GREAT advice.

I have decided to go with the easier 'select' fields rather than complicating things radio buttons.

A form with 2,000 + lines of code would get even messier adding javascript and database queries :)