How do i validate radio button? PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TheDumbNerd
Forum Newbie
Posts: 13
Joined: Sun Nov 13, 2011 4:01 pm

How do i validate radio button? PHP

Post 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.
bampera
Forum Newbie
Posts: 2
Joined: Sun Nov 13, 2011 7:51 pm

Re: How do i validate radio button? PHP

Post by bampera »

check $_POST['semester'] value. if > 0 display error message ;)
TheDumbNerd
Forum Newbie
Posts: 13
Joined: Sun Nov 13, 2011 4:01 pm

Re: How do i validate radio button? PHP

Post by TheDumbNerd »

bampera wrote:check $_POST['semester'] value. if > 0 display error message ;)
I don't understand what you mean by that?
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: How do i validate radio button? PHP

Post 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; }
}
TheDumbNerd
Forum Newbie
Posts: 13
Joined: Sun Nov 13, 2011 4:01 pm

Re: How do i validate radio button? PHP

Post 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?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do i validate radio button? PHP

Post 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 
}
Post Reply