Forum Moderators: coopster
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]
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?
if(isset($_SESSION['drink']) == '')
------
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...