Robert Granlund
asked on
Retain Value ofRadio Button
<input type="radio" name="rest_on_email" value="no"<?php if(isset($trimmed["rest_on_email"])) {echo 'checked="checked"';} ?>>YES
<input type="radio" name="rest_on_email" value="1" <?php if(isset($trimmed["rest_on_email"])) {echo 'checked="checked"';} ?>>NO
This is what I want to do but if gives me an error. Basically what I want to do is make the radio button sticky, so if someone does not complete the form, the choice will still be checked,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I see two problems. Where is the value of $trimmed["rest_on_email"] coming from or set?
You have two radio buttons and you are trying to check them both if $trimmed["rest_on_email"] is set. Once the form is submitted the radio button that is checked will be sent to the forms processor. If $trimmed[] is an array of trimmed values sent by the form, then only one of these radio buttons should appeared in that array.
You have two radio buttons and you are trying to check them both if $trimmed["rest_on_email"] is set. Once the form is submitted the radio button that is checked will be sent to the forms processor. If $trimmed[] is an array of trimmed values sent by the form, then only one of these radio buttons should appeared in that array.
@elvin66: Good point. Unless the client changes the default selection, the value that is submitted will always be "1" because it is the last instance of the matching name= attribute.
Thanks Ray. Yeah it seems he is trying to set both radios to "checked" here. Since moving to Florida in Feb this year I haven't done any webpage stuff at all just back line class scripting but it's obvious where this solution lays :)
ASKER
This is what I had come up with:
<input type="radio" name="rest_on_email" value="no"<?php if(isset($_POST['rest_on_email']) && $_POST['rest_on_email']=='no') { echo 'checked="checked"'; } else { echo 'checked="checked"';}?> >NO
<input type="radio" name="rest_on_email" value="1" <?php if(isset($_POST['rest_on_email']) && $_POST['rest_on_email']=='1') { echo 'checked="checked"'; }?> >YES
Open in new window