Page 1 of 1

How to retain value of a radio button

Posted: Wed Oct 14, 2009 12:18 am
by edawson003
How can code a radio button such that it retains it's value (checked) after a unsuccessful form post?

Here's how setup a a text field to retain value. I figure for radios, it should be too far off.

Code: Select all

<input class=clp type="text" name="exname" maxlength="50" size="40" value="<?php echo isset($exname) ? $exname : ''; ?>" />

Code: Select all

<input type="radio" name="extype" value="1">
<input type="radio" name="extype" value="2">
<input type="radio" name="extype" value="3">

Re: How to retain value of a radio button

Posted: Wed Oct 14, 2009 12:58 am
by Mirge
edawson003 wrote:How code a radio button such that it retains it's value (checked) after a unsuccessful form post?

Code: Select all

<input class=clp type="text" name="exname" maxlength="50" size="40" value="<?php echo isset($exname) ? $exname : ''; ?>" />

Code: Select all

<input type="radio" name="extype" value="1">
<input type="radio" name="extype" value="2">
<input type="radio" name="extype" value="3">
Well, one way would be to put an if() statement in each of the input radio's... for example (assuming form's method is POST):

Code: Select all

 
<input type="radio" name="extype" value="1"<?php if($_POST['extype'] == '1') { print "checked "; } ?>>
<input type="radio" name="extype" value="2"<?php if($_POST['extype'] == '2') { print "checked "; } ?>>
<input type="radio" name="extype" value="3"<?php if($_POST['extype'] == '3') { print "checked "; } ?>>
 
If you're using method="GET" instead, replace $_POST with $_GET.

Re: How to retain value of a radio button

Posted: Wed Oct 14, 2009 1:29 am
by edawson003
works perfect! Thanks!

Re: How to retain value of a radio button

Posted: Wed Oct 14, 2009 1:35 am
by Mirge
Not a problem. 8)