Page 1 of 1

Validate check box ?

Posted: Sun Jan 24, 2010 5:49 am
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(); 
 
 

Re: Validate check box ?

Posted: Sun Jan 24, 2010 6:27 am
by requinix

Code: Select all

isset($_POST["terms_box"])
Gives true if the checkbox was checked.

Re: Validate check box ?

Posted: Sun Jan 24, 2010 7:02 am
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

Re: Validate check box ?

Posted: Sun Jan 24, 2010 7:42 am
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