Forum Moderators: coopster

Message Too Old, No Replies

Retaining radio button value

I'm showing you mine ... what's yours

         

henry0

4:20 pm on Nov 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I was looking to retain radio button value when the form is sent back due to filtering action.
One could always go back and forth to a DB (first visit will be an insert then multiple updates)
But I do not like that option too much
So I came with the following (what do you use?)
Note: it is important to retain the values since that form has about 60+ of such radio buttons

Has is it does well and does not generate any error with error E_ALL on for debugging purpose, proving that all var are declared.

FORM:
// top of form: anywhere below session_start();
if(empty($_SESSION['health']) &&!isset($_SESSION['health'])){$health="";}else{$health=$_SESSION['health'];}

// anywhere in the form
/////////////// health ///////////////////
if(empty($health)){$y=""; $n="";}
else
{
switch($health)
{
case "y":
$y="checked";
$n="";
break;
case "n";
$y="";
$n="checked";
break;
}
}
echo"<tr>
<td align='left' width='70%'><div id='strong_light'>Is the patient in good health:</div></td>
<td align='left' width='15%'>&nbsp; Yes<input type=radio name=health value='y' $y>&nbsp;</td>
<td align='left' width='15%'>&nbsp; No<input type=radio name=health value='n' $n>&nbsp;
</td></tr>";
////////////// end of health /////////////

MrGecko

6:40 pm on Nov 8, 2007 (gmt 0)

10+ Year Member



I'm not exactly sure I understand your question, but if you want to use sessions I would use this:

<?
session_start();
$health = isset($_SESSION['health'])? $_SESSION['health'] : "";

echo"<tr>
<td align='left' width='70%'><div id='strong_light'>Is the patient in good health:</div></td>
<td align='left' width='15%'>&nbsp; Yes<input type=radio name=health value='y'";
if ($health == "y") {
echo " checked";
}
echo ">&nbsp;</td>
<td align='left' width='15%'>&nbsp; No<input type=radio name=health value='n'";
if ($health == "n") {
echo " checked";
}
echo ">&nbsp;
</td></tr>";
?>

If you want to get information from a previous form I would use this:

<?
$health = isset($_REQUEST['health'])? $_REQUEST['health'] : "";

echo"<tr>
<td align='left' width='70%'><div id='strong_light'>Is the patient in good health:</div></td>
<td align='left' width='15%'>&nbsp; Yes<input type=radio name=health value='y'";
if ($health == "y") {
echo " checked";
}
echo ">&nbsp;</td>
<td align='left' width='15%'>&nbsp; No<input type=radio name=health value='n'";
if ($health == "n") {
echo " checked";
}
echo ">&nbsp;
</td></tr>";
?>

eelixduppy

6:55 pm on Nov 8, 2007 (gmt 0)



As far as brevity is concerned, I would use a ternary conditional:

Yes <input type="radio" name="health" value="y" <?php echo ($health == 'y')? 'checked':'';?>>

henry0

6:58 pm on Nov 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's not a question my way works fine
I am showing a solution that does well

and simply ask what other coders do to retain radio values
it's a topic that comes often and not too many solutions are around

henry0

7:01 pm on Nov 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



eelix, that's a good update.

I am guilty of not enough thinking about the ternary