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">

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

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

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

As usually the solution is simple, using isset() method:
if(isset($_POST['newsletter']))
 $newsletter= 1;
else
 $newsletter= 0;

No comments:

Post a Comment