[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Retain Value ofRadio Button

Posted on 2012-09-19
6
Medium Priority
?
252 Views
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,
0
Comment
Question by:rgranlund
6 Comments
 
LVL 83

Expert Comment

by:leakim971
ID: 38415051
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

0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 38415775
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
error_reporting(E_ALL);

// SET THE SIGNAL
$trimmed["rest_on_email"] = 'X';
?>
<form>
<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" />
</form>

Open in new window

0
 
LVL 20

Expert Comment

by:Mark Brady
ID: 38415802
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38415865
@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.
0
 
LVL 20

Expert Comment

by:Mark Brady
ID: 38416348
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 :)
0
 
LVL 7

Author Closing Comment

by:rgranlund
ID: 38418478
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

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmakerā€¦
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month19 days, 5 hours left to enroll

834 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