/    Sign up×
Community /Pin to ProfileBookmark

Keep a radio button value

How would you keep a radio buttons value? When submitting a form using PHP_SELF??

[code=php]<label>Reason for Contact: </label>
<label><input type=”radio” name=”reason” value=”<?php if(isset($reason)) { $_POST[‘radio’]; } ?>” />Query</label>
<label><input type=”radio” name=”reason” value=”radio” />Complaint</label>
<label><input type=”radio” name=”reason” value=”radio” />Suggestion</label>
<label><input type=”radio” name=”reason” value=”radio” />Other</label></p>[/code]

Same or similar too?

[code=php]<label>Your Full Name: </label>
<input name=”name” type=”text” size=”20″ maxlength=”64″ value=”<?php if(isset($name)) { echo $_POST[‘name’]; }?>” /></p>[/code]

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 28.2010 — I'd probably do something like:
[code=php]
$values = array('Query','Complaint','Suggestion','Other');
foreach($values as $value)
{
$checked = (isset($_POST['reason']) && $_POST['reason'] == $value) ?
" checked='checked'" : '';
printf(
"<label><input type='radio' name='reason' value='&#37;s'%s />%s</label>n",
htmlspecialchars($value, ENT_QUOTES),
$checked,
htmlspecialchars($value, ENT_QUOTES)
);
}
[/code]
Copy linkTweet thisAlerts:
@Hooded_VillianauthorFeb 28.2010 — [code=php]<label><input type="radio" name="reason" value="radio" <?php echo (isset($_POST['reason']) && $_POST['reason']=='radio') ? 'checked' : '';?> />Query</label>
<label><input type="radio" name="reason" value="radio" <?php echo (isset($_POST['reason']) && $_POST['reason']=='radio') ? 'checked' : '';?>/>Complaint</label>
<label><input type="radio" name="reason" value="radio" <?php echo (isset($_POST['reason']) && $_POST['reason']=='radio') ? 'checked' : '';?>/>Suggestion</label>
<label><input type="radio" name="reason" value="radio" <?php echo (isset($_POST['reason']) && $_POST['reason']=='radio') ? 'checked' : '';?>/>Other</label></p>[/code]


But for some reason... It always displays the "other" radio button upon page reloading...
Copy linkTweet thisAlerts:
@NogDogMar 01.2010 — You have the a value of "radio" for each button. The values should be different, to reflect what the user selected.
Copy linkTweet thisAlerts:
@Hooded_VillianauthorMar 01.2010 — I got it working the way I want it too... Except for one little thing.

[code=php]
$reason = 'unchecked';

if(!isset($_POST['reason']))
{ $error8 = true;
$output_form = true;
}

$to = '[email protected]';
$title = "Contact Page";
$msg = "Name: " . $name . "nEmail: " .$email. "nText: " .$text;
$from = $name;

//send the email
mail($to, $title, $msg, $from);

//confirm mail was sent
echo '<div id="thanks"><p>Thank you ' .$name. ' for your contacting us. We will be in touch shortly, if it is neccessary.</div>';
echo '<p><a href="default.php" class="link">back</a></p>';

<label>Reason for Contact: </label>
<label><input type="radio" name="reason" value="query" <?php echo (isset($_POST['reason']) && $_POST['reason']=='query') ? 'checked' : '';?> />Query</label>
<label><input type="radio" name="reason" value="complaint" <?php echo (isset($_POST['reason']) && $_POST['reason']=='complaint') ? 'checked' : '';?> />Complaint</label>
<label><input type="radio" name="reason" value="suggestion" <?php echo (isset($_POST['reason']) && $_POST['reason']=='suggestion') ? 'checked' : '';?> />Suggestion</label>
<label><input type="radio" name="reason" value="other" <?php echo (isset($_POST['reason']) && $_POST['reason']=='other') ? 'checked' : '';?> />Other</label></p>
[/code]


How would I make the $title take the value of the clicked radio button? So that when the email comes through, it will come through as either Query, Complaint etc..

TIA...
Copy linkTweet thisAlerts:
@MindzaiMar 03.2010 — [code=php]$_POST['reason'][/code]

Also make sure you are cleaning the user input otherwise you will be open to (at least) header injection attacks. And if your doctype is one of the XHTML family you need to use 'checked="checked"' instead of just 'checked'.
Copy linkTweet thisAlerts:
@Hooded_VillianauthorMar 04.2010 — [code=php]$_POST['reason'][/code]

Also make sure you are cleaning the user input otherwise you will be open to (at least) header injection attacks. And if your doctype is one of the XHTML family you need to use 'checked="checked"' instead of just 'checked'.[/QUOTE]


I will have to check that out. $field=''; should do the trick, right? To clear the fields?

As for my doctype being XHTML, I think it is. (Currently replying from my BlackBerry) Will check it @ home.

So does that mean... <?php echo (isset($_POST['reason']) && $_POST['reason']=='query') ? 'checked' : '';?> needs to change to <?php echo (isset($_POST['reason']) && $_POST['reason']=='query') ? 'checked="checked"' : '';?>
×

Success!

Help @Hooded_Villian spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...