SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Oct 1999
    Location
    New York, NY
    Posts
    826
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    radio button - keep selected if error?

    Hi,

    I have a form with several fields, one being a radio box (IE. male | female). If there is an error upon submission, I figured out how to get all the input text boxes to throw back the inserted info for the user while spelling out the error message:
    PHP Code:
    First name:<input type="Text" name="first" value="<?php echo $first ?>">
    But I can't seem to figure out how to do the same for radio boxes! I want the user's selection to remain in the output if there is an error message. My current code is simply:
    PHP Code:
    <input type="radio" name="gender" value="male"Male | <input type="radio" name="gender" value="female"Female 
    And I tried using the same php echo command but that didn't seem to do it. Thanks for the help!

  2. #2
    SitePoint Addict
    Join Date
    May 2003
    Location
    Auckland
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the "value" attribute of the radio button / checkbox has nothing to do with what field is actually checked.

    You need to add checked="checked" to the relevant input field.

    Code:
    <input type="radio" name="gender" value="male" checked="checked" /> Male
    You should use labels on your form elements (especially radio buttons / checkboxes) to make them easier to select. Give each field a unique id and put that in the labels "for" attribute.

    Code:
    <input type="radio" id="MaleGenderRadioButton" name="gender" value="male" checked="checked" /> <label for="MaleGenderRadioButton">Male</label>

  3. #3
    SitePoint Guru
    Join Date
    Oct 1999
    Location
    New York, NY
    Posts
    826
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    Thanks for the info... however, since it's a male/female radio box, I'd rather have neither of them checked by default. Even so, how can I keep the users choice if the error message is returned? (IE. using the php echo command to fill in the text boxes so they don't lose their data)

  4. #4
    SitePoint Guru
    Join Date
    Oct 1999
    Location
    New York, NY
    Posts
    826
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Me again... I also notices that when an error message is displayed, I need to return the value selected by the user in a drop-drown menu. Any help regarding this is appreciated.

    I'm currently using:
    Code:
     
    First name:<input type="Text" name="first" value="<?php echo $first ?>">
    to return the entered text in a text box, but can't figure out how to keep the selected data in tact from a drop-down or radio button if an error message is returned.

    Current code:
    Code:
     
    Location:
    	<SELECT name="location" SIZE="1">
    	<OPTION SELECTED>--choose--
    	<OPTION>Los Angeles
    	<OPTION>New York
    	<OPTION>Other
    	</SELECT>
    and
    Code:
    Specify:<input type="radio" id="MaleGenderRadioButton" name="gender" value="male" /> <label for="MaleGenderRadioButton">Male</label> | <input type="radio" id="FemaleGenderRadioButton" name="gender" value="female" /> <label for="FemaleGenderRadioButton">Female</label>
    Thanks.

  5. #5
    SitePoint Evangelist jplush76's Avatar
    Join Date
    Nov 2003
    Location
    Los Angeles, CA
    Posts
    460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this should work for you
    put this code anywhere above the radio buttons

    first of all try to never use superglobals, IE $gender instead use the form's method $_POST['gender'] (or GET if you're using GET)

    PHP Code:
    <?php
    $male_checked 
    = (isset($_POST['gender']) && $_POST['gender'] == 'male') ? 'checked' '';

    $female_checked = (isset($_POST['gender']) && $_POST['gender'] == 'female') ? 'checked' '';
    ?>

    HTML Code:
    <input type="radio" name="gender" value="male" <?=$male_checked?>> Male | <input type="radio" name="gender" value="female" <?=$female_checked?>> Female
    that should give you what you need...
    first page load no buttons will be selected... if the user selects one and there is an error it will re-load the page with the checked item selected.
    My-Bic - Easiest AJAX/PHP Framework Around
    Now Debug PHP scripts with Firebug!


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
  •