Page 1 of 1
How do i validate radio button? PHP
Posted: Sun Nov 13, 2011 8:18 pm
by TheDumbNerd
<form name="Stumarks" action="lala.php" method="GET">
Semester: <br />
<input type="radio" name="semester" value="1" />Fall<br />
<input type="radio" name="semester" value="2" />Spring<br />
<input type="submit" value="submit"/>
</form>
I want those to be validated so that if the user did not select one, then the data will be redirected to this original form with an error message beside the radio buttons.
Re: How do i validate radio button? PHP
Posted: Sun Nov 13, 2011 8:29 pm
by bampera
check $_POST['semester'] value. if > 0 display error message

Re: How do i validate radio button? PHP
Posted: Sun Nov 13, 2011 8:39 pm
by TheDumbNerd
bampera wrote:check $_POST['semester'] value. if > 0 display error message

I don't understand what you mean by that?
Re: How do i validate radio button? PHP
Posted: Sun Nov 13, 2011 8:43 pm
by twinedev
check $_POST['semester'] value. if > 0 display error message
Two problems with that:
1. if ($_POST['semester'] > 0, that means they DID pick one, not an error.
2. If they do not pick any radio buttons, $_POST['semester'] will NOT exist when you submit the form. (same for checkboxes BTW)
Most basic:
Code: Select all
if (!isset[$_POST['semester']) {
// ERROR
}
if you are assigning it to another variable first though:
Code: Select all
$intSemester = (isset($_POST['semester'])) ? (int)$_POST['semester'] : FALSE;
if (!$intSemester) {
// ERROR
}
If you have a bunch of radios / checkboxes on the form, try the following right at the top before validating. The values in array() are a list of radio/checkbox names
Code: Select all
foreach(array('semester','year','accept-terms') as $index) {
if (!isset($_POST[$index])) { $_POST[$index] = FALSE; }
}
Re: How do i validate radio button? PHP
Posted: Sun Nov 13, 2011 8:59 pm
by TheDumbNerd
twinedev wrote:check $_POST['semester'] value. if > 0 display error message
Two problems with that:
1. if ($_POST['semester'] > 0, that means they DID pick one, not an error.
2. If they do not pick any radio buttons, $_POST['semester'] will NOT exist when you submit the form. (same for checkboxes BTW)
Most basic:
Code: Select all
if (!isset[$_POST['semester']) {
// ERROR
}
if you are assigning it to another variable first though:
Code: Select all
$intSemester = (isset($_POST['semester'])) ? (int)$_POST['semester'] : FALSE;
if (!$intSemester) {
// ERROR
}
If you have a bunch of radios / checkboxes on the form, try the following right at the top before validating. The values in array() are a list of radio/checkbox names
Code: Select all
foreach(array('semester','year','accept-terms') as $index) {
if (!isset($_POST[$index])) { $_POST[$index] = FALSE; }
}
What about if I wanted to show an error message if the user did not select any of the options?
Re: How do i validate radio button? PHP
Posted: Sun Nov 13, 2011 9:11 pm
by Celauran
TheDumbNerd wrote:What about if I wanted to show an error message if the user did not select any of the options?
twinedev wrote:
2. If they do not pick any radio buttons, $_POST['semester'] will NOT exist when you submit the form. (same for checkboxes BTW)
Most basic:
Code: Select all
if (!isset[$_POST['semester']) {
// ERROR
}