Monday, February 20, 2012

How to verify if checkbox was checked in PHP after submit?

Often when you have a form in a html file(might be even php file as you might know) and submit to a php file you want to see if checkbox from your form was checked so you will save in your database accordingly some value.
I have a form like this:

<form name="AddUser" method="post" action="createUser.php">

<legend>Add a user </legend>
<label for="name">Name</label>
<input type="text" name="name" id="name" value="" />

<label for="newsletter">Subscribe to newsletter:</label>
<input type="checkbox" name="newsletter" id=" newsletter " value="" />

<input type="submit" name="add_user_btn" value="Create" />

As usually the solution is simple, using isset() method:
 $newsletter= 1;
 $newsletter= 0;

No comments:

Post a Comment