Retain Value ofRadio Button

Posted on 2012-09-19
Last Modified: 2012-09-20
<input type="radio" name="rest_on_email" value="no"<?php if(isset($trimmed["rest_on_email"])) {echo 'checked="checked"';} ?>>YES 
<input type="radio" name="rest_on_email" value="1" <?php if(isset($trimmed["rest_on_email"])) {echo 'checked="checked"';} ?>>NO

Open in new window

This is what I want to do but if gives me an error.  Basically what I want to do is make the radio button sticky, so if someone does not complete the form, the choice will still be checked,
Question by:rgranlund
    LVL 81

    Expert Comment

    Add a space before checked : ' checked="checked"'
    <input type="radio" name="rest_on_email" value="no"<?php if(isset($trimmed["rest_on_email"])) {echo ' checked="checked"';} ?>>YES 
    <input type="radio" name="rest_on_email" value="1" <?php if(isset($trimmed["rest_on_email"])) {echo ' checked="checked"';} ?>>NO

    Open in new window

    LVL 107

    Accepted Solution

    Are you sure these are in the right order?  If I click on the button next to "Yes" it sends "no" and when I click the "No" button it sends "1" -- that seems a little counterintuitive.

    <?php // RAY_temp_rgranlund.php
    $trimmed["rest_on_email"] = 'X';
    <input type="radio" name="rest_on_email" value="no" <?php if(isset($trimmed["rest_on_email"])) {echo 'checked="checked"';} ?> />YES
    <input type="radio" name="rest_on_email" value="1"  <?php if(isset($trimmed["rest_on_email"])) {echo 'checked="checked"';} ?> />NO
    <input type="submit" />

    Open in new window

    LVL 20

    Expert Comment

    by:Mark Brady
    I see two problems. Where is the value of $trimmed["rest_on_email"] coming from or set?

    You have two radio buttons and you are trying to check them both if $trimmed["rest_on_email"] is set. Once the form is submitted the radio button that is checked will be sent to the forms processor. If $trimmed[] is an array of trimmed values sent by the form, then only one of these radio buttons should appeared in that array.
    LVL 107

    Expert Comment

    by:Ray Paseur
    @elvin66: Good point.  Unless the client changes the default selection, the value that is submitted will always be "1" because it is the last instance of the matching name= attribute.
    LVL 20

    Expert Comment

    by:Mark Brady
    Thanks Ray. Yeah it seems he is trying to set both radios to "checked" here. Since moving to Florida in Feb this year I haven't done any webpage stuff at all just back line class scripting but it's obvious where this solution lays :)
    LVL 7

    Author Closing Comment

    This is what I had come up with:

    <input type="radio" name="rest_on_email" value="no"<?php if(isset($_POST['rest_on_email']) && $_POST['rest_on_email']=='no') { echo 'checked="checked"'; } else { echo 'checked="checked"';}?> >NO
    <input type="radio" name="rest_on_email" value="1" <?php if(isset($_POST['rest_on_email']) && $_POST['rest_on_email']=='1') { echo 'checked="checked"'; }?> >YES

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
    Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now