SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Oct 2002
    Posts
    311
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Radio *argh* Buttons and variables?

    Hey,

    I've got an online form with text fields for user details and then radio buttons for answers to questions. Now, if the user misses something It refreshes the form and point out what is missing. NOW, at the moment I can keep the information the user previously inputted in the text boxes by putting the value of the text box to:

    value="<?php echo $first_name; ?>"

    Now how do I get it to remember what was selected in a radio group? Do you know what I mean? It's very frustrating for users to have to re answer everything just because they may have missed one thing.

    Cheers,
    Chris

  2. #2
    SitePoint Evangelist Mr. Brownstone's Avatar
    Join Date
    May 2002
    Location
    Relative
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can make a Radio Button “checked” by inserting the following parameter:
    Code:
    <input type="radio" name="myradiobutton" value="selected1" checked="checked" />
    <input type="radio" name="myradiobutton" value="selected2" />
    This also applies for Check Boxes. (type="checkbox")

    Now when you do a form request for “myradiobutton”, the value “selected1” or “selected2” will be returned depending on which button was checked.
    Last edited by Mr. Brownstone; Mar 17, 2003 at 10:51. Reason: Didn’t read the question fully
    Of course, that's just my opinion. I could be wrong.

  3. #3
    SitePoint Addict
    Join Date
    Oct 2002
    Posts
    311
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok but the value field is already taken:

    Code:
    <td><div align="center"> Before 9am 
    					 <input type="radio" name="CallYou" value="before 9am">
    					 </div></td>
    					 <td><div align="center">9-12 noon 
    					 <input type="radio" name="CallYou" value="9-12 noon">
    					 </div></td>
    					 <td><div align="center">12-5pm 
    					 <input type="radio" name="CallYou" value="12-5pm">
    					 </div></td>
    					 <td><div align="center">5-9pm 
    					 <input type="radio" name="CallYou" value="5-9pm">
    </div></td>
    So where am I meant to put it?

    This is how I have the text field:

    Code:
    <input name="first_name" type="text" id="first_name" value="<?php echo $first_name; ?>">
    Can't see where it goes m8.

  4. #4
    SitePoint Evangelist Mr. Brownstone's Avatar
    Join Date
    May 2002
    Location
    Relative
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I hope this makes sense:
    PHP Code:
    <?php

        $BeforeNine 
    '';
        
    $Morning '';
        
    $Afternoon '';
        
    $Evening '';

        if ( 
    $CallYou == "before 9am" ) {
            
    $BeforeNine ' checked="checked"';
        }

        if ( 
    $CallYou == "9-12 noon" ) {
            
    $Morning ' checked="checked"';
        }

        if ( 
    $CallYou == "12-5pm" ) {
            
    $Afternoon ' checked="checked"';
        }

        if ( 
    $CallYou == "5-9pm" ) {
            
    $Evening ' checked="checked"';
        }

    ?>

    <td>
        <div align="center">
            Before 9am
            <input type="radio" name="CallYou" value="before 9am"<?php echo $BeforeNine ?>>
        </div>
    </td>
    <td>
        <div align="center">
            9-12 noon 
            <input type="radio" name="CallYou" value="9-12 noon"<?php echo $Morning ?>>
        </div>
    </td>
    <td>
        <div align="center">
            12-5pm 
            <input type="radio" name="CallYou" value="12-5pm"<?php echo $Afternoon ?>>
        </div>
    </td>
    <td>
        <div align="center">
            5-9pm 
            <input type="radio" name="CallYou" value="5-9pm"<?php echo $Evening ?>>
        </div>
    </td>

    <?php

        
    // . . . blah blah


    ?>
    Of course, that's just my opinion. I could be wrong.

  5. #5
    SitePoint Addict
    Join Date
    Oct 2002
    Posts
    311
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Worked a treat

    Cheers!

  6. #6
    SitePoint Member tahjah's Avatar
    Join Date
    Mar 2003
    Location
    US
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could also place the if conditional in the input tag. It makes it easy to add or replicate the input boxes with regular expressions.
    Code:
     
    <input type="radio" name="myradiobutton" value="selected1" <?php if ($_POST['myradiobutton'] == "selected1") echo 'checked="checked"'; ?> /> 
    <input type="radio" name="myradiobutton" value="selected2" <?php if ($_POST['myradiobutton'] == "selected2") echo 'checked="checked"'; ?> />
    If you wanted to set one as the default, then add || $_POST['myradiobutton'] == "" to the conditional, so it will be selected the first time through if you use postbacks. You can also modify this for checkboxes by checking if the value exists in the string.

  7. #7
    SitePoint Addict
    Join Date
    Oct 2002
    Posts
    311
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One more question, how would you use this method for a List/Menu with the following code:

    Code:
    <select name="MostImportant">
    	   <option value="Extra Income">Extra Income</option>
    	   <option value="Financial Freedom">Financial Freedom</option>
    </select>
    I tried using the same as the radio button method, but it didn't work.

  8. #8
    SitePoint Evangelist Mr. Brownstone's Avatar
    Join Date
    May 2002
    Location
    Relative
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use selected="selected" to set an <option>. For example:
    PHP Code:
    <?php

    $ExtraIncome 
    '';
    $FinancialFreedom '';

    if ( 
    $MostImportant == 'Extra Income' ) {
        
    $ExtraIncome ' selected="selected"';
    }

    if ( 
    $MostImportant == 'Financial Freedom' ) {
        
    $FinancialFreedom ' selected="selected"';
    }

    ?>

    <select name="MostImportant">
           <option value="Extra Income"<?php echo $ExtraIncome ?>>Extra Income</option>
           <option value="Financial Freedom"<?php echo $FinancialFreedom ?>>Financial Freedom</option>
    </select>
    Of course, that's just my opinion. I could be wrong.


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
  •