Validate check box ?

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
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Validate check box ?

Post by MiniMonty »

Hi all,

I have a simple form to insert users into a DB.
This works fine but I also have an unchecked checkbox which I require users to check
(or error message). What do I need to add to the code below to validate the checkbox ?
Checkbox is called "terms_box".
Thanks all.

Best wishes
Monty

Code: Select all

 
if (isset ($_POST['SignUp'])){
 
     $sql = mysql_query("INSERT INTO `students_to_courses` ( `id` , `student_fk` , `course_fk` , `course_completed` ) 
    VALUES ('', '$id', '2', '0')")
     or die (mysql_error());
     $id = mysql_insert_id(); 
 
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Validate check box ?

Post by requinix »

Code: Select all

isset($_POST["terms_box"])
Gives true if the checkbox was checked.
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Re: Validate check box ?

Post by MiniMonty »

Sweet and simple - thanks.

So now how do I write an "else if" which targets only the checkbox and not the
$_POST of the submit button ?

(It's a terms and conditions checkbox next to the submit button).

Best wishes
Monty
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Validate check box ?

Post by requinix »

Maybe you're not quite sure how to use if blocks?

If the form was submitted,
- If the checkbox was not checked,
- - Show a message
- Else,
- - Do whatever
Else,
- Do whatever
Post Reply