SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru tictike's Avatar
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    863
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    is there a better way to check a radio button after the form is submitted

    I'm trying to validate a form. If after it's submitted and there is a problem with the email field for example, but not this radio group, I want the page to be served with whatever radio button was select, to include "checked='checked'". I started with this markup

    Code HTML4Strict:
    <div>
    <input type="radio" name="rate" value="excellent" id="ex" class="radio"> 
    <label for="ex">Excellent</label>
    </div>
    <div>
    <input type="radio" name="rate" value="good" id="go">
    <label for="go">Good</label>
    </div>
    <div>
    <input type="radio" name="rate" value="fair" id="fa">
    <label for="fa">Fair</label>
    </div>
    <div>
    <input type="radio" name="rate" value="poor" id="po">
    <label for="po">Poor</label>
    </div>

    I originally thought I would have to add this logic to every input:

    Code PHP:
    <?php 
    	if($_POST['rate'] == "excellent"){
    echo "<input type='radio' name='rate' value='excellent' id='ex' class='radio' checked='checked'>"; 
    }
    else{
    echo "<input type='radio' name='rate' value='excellent' id='ex' class='radio'>"; 
    }
    ?>

    something tells me there is a much better technique..

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php if($_POST['rate'] == "excellent") echo ' checked="checked"'?>
    If you have a lot of radio buttons, you could consider generating their html dynamically, and possibly making some functions to help.

  3. #3
    SitePoint Guru tictike's Avatar
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    863
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh ok, so I could put these 4 lines of php in the input elements, respectively?

    <?php if($_POST['rate'] == "excellent") echo ' checked="checked"'; ?>
    <?php if($_POST['rate'] == "good") echo ' checked="checked"'; ?>
    <?php if($_POST['rate'] == "fair") echo ' checked="checked"'; ?>
    <?php if($_POST['rate'] == "poor") echo ' checked="checked"'; ?>

    Like this?
    <input type="radio" name="rate" value="poor" id="po" <?php if($_POST['rate'] == "poor") echo ' checked="checked"'; ?> >

  4. #4
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this-
    PHP Code:
      <tr>
            <td width="30%" class="loginText">rate: </td>
            <td width="70%" class="profilText"><?php

                              
    if($rate== "excellent"){
                                
    $genderv "excellent";
                              }
                              else if(
    $rate== "good"){
                                
    $genderv "good";
                              }
                                             else if(
    $rate== "fair"){
                                
    $genderv "fair";
                              }
                                                else if(
    $rate== "poor"){
                                
    $genderv "poor";
                              }

                              
    $genderval selectVal($genderv);

                              
    ?>
                              <input type="radio" name="rate" value="excellent" <?=$genderval[0];?>>excellent &nbsp;&nbsp;
                               <input type="radio" name="rate" value="good" <?=$genderval[1];?>>good&nbsp;&nbsp;
     <input type="radio" name="rate" value="fair" <?=$genderval[2];?>>fair&nbsp;&nbsp;
     <input type="radio" name="rate" value="poor" <?=$genderval[3];?>>poor&nbsp;&nbsp;
                              </td>
                            </tr>
    Let us know what you get with this code.

  5. #5
    SitePoint Guru tictike's Avatar
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    863
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the suggestion. I am just learning so I'm more comfortable with a solution similar to what crmalibu suggested.

    Right now I have this:
    <div>
    <input type="radio" name="rate" value="excellent" id="ex" class="radio" checked="<?php if($_POST['rate'] == "excellent") echo "checked"; ?>">
    <label for="ex">Excellent</label>
    </div>
    <div>
    <input type="radio" name="rate" value="good" id="go" checked="<?php if($_POST['rate'] == "good") echo "checked"; ?>">
    <label for="go">Good</label>
    </div>
    <div>
    <input type="radio" name="rate" value="fair" id="fa" checked="<?php if($_POST['rate'] == "fair") echo "checked"; ?>">
    <label for="fa">Fair</label>
    </div>
    <div>
    <input type="radio" name="rate" value="poor" id="po" checked="<?php if($_POST['rate'] == "poor") echo "checked"; ?>">
    <label for="po">Poor</label>
    </div>

    and the last radio button is always checked, even on first page load. What did i do wrong? If I switch the 'fair' input and label with the 'poor' input and label, then 'fair' is last and always checked..

  6. #6
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Most browsers will check a radio with the following markup
    Code HTML:
    <input type="radio" name="blah" value="1234" checked>

    Which means that checked="" is still going to result in a checked radio. The last takes precedence.

    You could change your code to this:
    PHP Code:
    <div>
    <input type="radio" name="rate" value="poor" id="po" <?php if($_POST['rate'] == "poor") echo "checked='checked'"?>>
    <label for="po">Poor</label>
    </div>
    Personally I'd build a loop though, I think the code would look a bit tidier.

  7. #7
    SitePoint Guru tictike's Avatar
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    863
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, thanks! I'm so used to giving attributes a value I forgot about just 'checked'.


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
  •