SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
-
Aug 8, 2007, 20:54 #1
- Join Date
- Aug 2004
- Location
- Cairns, Australia
- Posts
- 762
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
remembering radio button (form validation)
Hi guys,
I am trying to get my form to remember the value of the radio buttons during validation.
Is there a more efficient way of checking the radio button that was originally selected?
I am storing the selection in a session upon submission of the form.
PHP Code:<?
if ($_SESSION['gender'] == 'male')
{
?>
<tr><td valign="center">Gender:</td>
<td valign="center"><input type="radio" name="gender" class="radio" value="male" style="background:none; border:none;" checked="checked" />male
<input type="radio" name="gender" class="radio" value="female" style="background:none; border:none;" />female</td></tr>
<?
} elseif ($_SESSION['gender'] == 'female')
{
?>
<tr><td valign="center">Gender:</td>
<td valign="center"><input type="radio" name="gender" class="radio" value="male" style="background:none; border:none;" />male
<input type="radio" name="gender" class="radio" value="female" style="background:none; border:none;" checked="checked" />female</td></tr>
<?
} elseif ($_SESSION['gender'] == '')
{
?>
<tr><td valign="center">Gender:</td>
<td valign="center"><input type="radio" name="gender" class="radio" value="male" style="background:none; border:none;" />male
<input type="radio" name="gender" class="radio" value="female" style="background:none; border:none;" />female</td></tr>
<?
}
?>
-
Aug 8, 2007, 21:15 #2
- Join Date
- Jun 2007
- Location
- North Richmond
- Posts
- 495
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You can input this code and it should work. It saves you duplicating the html as the condition is really only for the 'checked' status.
PHP Code:<tr>
<td valign="center">Gender:</td>
<td valign="center">
<input type="radio" name="gender" class="radio" value="male"
style="background:none; border:none;"
<? if($_POST['gender'] == "Male"){ echo 'checked="checked"';}?> />male
<input type="radio" name="gender" class="radio" value="female"
style="background:none; border:none;"
<? if($_POST['gender'] == "Female"){ echo 'checked="checked"';}?> />female</td>
</tr>
Cheers
-
Aug 8, 2007, 21:27 #3
- Join Date
- Oct 2006
- Location
- Kathmandu, Nepal
- Posts
- 4,013
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
If you are using session to store the already submitted value of the radio button then better do like this:
Code php:<tr> <td valign="center">Gender:</td> <td valign="center"> <input type="radio" name="gender" class="radio" value="male" style="background:none; border:none;" <? if($_SESSION['gender'] == "male"){ echo 'checked="checked"';}?> />male <input type="radio" name="gender" class="radio" value="female" style="background:none; border:none;" <? if($_SESSION['gender'] == "female"){ echo 'checked="checked"';}?> />female</td> </tr>
To save more time and you do not have problem selecting one radio button default selection, then you can only do with non-selected button only.Mistakes are proof that you are trying.....
------------------------------------------------------------------------
PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5
-
Aug 9, 2007, 00:08 #4
- Join Date
- Aug 2004
- Location
- Manchester UK
- Posts
- 13,807
- Mentioned
- 158 Post(s)
- Tagged
- 3 Thread(s)
PHP Code:echo 'checked="checked"';
PHP Code:echo ' checked';
Mike Swiffin - Community Team Advisor
Only a woman can read between the lines of a one word answer.....
-
Aug 9, 2007, 05:11 #5
- Join Date
- Feb 2007
- Location
- England, High Wycombe Bucks
- Posts
- 76
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I'm very new to PHP myself, so this is a question about this really.
I'm under the impression that PHP is writing a new attribute AND value to this input tag, is it therefore not correct to input checked="checked" as the attribute and value, as this is valid HTML. Forgive me, I've always learnt to code my checked code into HTML this way and I may have misunderstood something. It's that you said it 'should' be coded as checked that has bothered me. Is this something that only PHP understands?Wyte R@ven - Creator of the Rift
-
Aug 9, 2007, 05:22 #6
- Join Date
- Nov 2006
- Posts
- 71
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You simply put "checked" into the radio tag when you want it to be checked by default OR if you're doing form validation and you use PHP to "remember" values carried over if the form doesn't validate so the user doesn't have to re-enter/select all the values. Actual HTML syntax has practically nothing to do with PHP whatsoever.
-
Aug 9, 2007, 06:13 #7
- Join Date
- Aug 2004
- Location
- Manchester UK
- Posts
- 13,807
- Mentioned
- 158 Post(s)
- Tagged
- 3 Thread(s)
^ exactly
Form elements such as radio buttons, checkboxes etc are all standard HTML, PHP is only used to output the HTML so it has to conform to the coding logic and standards.Mike Swiffin - Community Team Advisor
Only a woman can read between the lines of a one word answer.....
Bookmarks