SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question 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 &nbsp;
        <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 &nbsp;
        <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 &nbsp;
        <input type="radio" name="gender" class="radio" value="female" style="background:none; border:none;" />female</td></tr>
              
       <?
        
        
    }
        
    ?>

  2. #2
    SitePoint Evangelist BJ Duncan's Avatar
    Join Date
    Jun 2007
    Location
    Bowen Mountain, NSW
    Posts
    490
    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 &nbsp;
                <input type="radio" name="gender" class="radio" value="female"
                style="background:none; border:none;"
                <? if($_POST['gender'] == "Female"){ echo 'checked="checked"';}?>  />female</td>
            </tr>
    Hope this helps
    Cheers

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    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 &nbsp;
                <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

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    PHP Code:
    echo 'checked="checked"'
    should be just
    PHP Code:
    echo ' checked'
    you don't need the checked=
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    SitePoint Enthusiast wyte raven's Avatar
    Join Date
    Feb 2007
    Location
    England, High Wycombe Bucks
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spikeZ View Post
    PHP Code:
    echo 'checked="checked"'
    should be just
    PHP Code:
    echo ' checked'
    you don't need the checked=
    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

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wyte raven View Post
    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?
    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.

  7. #7
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    157 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •