Add Checkboxes to the AJAX Contact Form

For all our AJAX Contact Form customers on CodeCanyon, here’s a quick tutorial on how to add / validate checkboxes.

The HTML

Place the following in your form (index.html in our package):

<label for="checkbox">
	<input type="checkbox" name="checkbox" id="checkbox" value="Milk">Milk
</label>

The JavaScript

This goes in the JavaScript file (jquery.jigowatt.js).

Before

$.post(action, {

Place some code to get the value of the checkbox:

var checkbox_value = '';

if (jQuery('input#checkbox').is(':checked'))
	checkbox_value = 1;
else
	checkbox_value = 0;

After

comments: $('#comments').val(),

Add the following to pass the value to the validation script:

checkbox: checkbox_value

The Validation

In contact.php you can check the value of the posted variable ‘checkbox’.

$checkbox = $_POST['checkbox'];

The value will be 1 or 0, depending on whether it was checked or not. From here you can then validate if its ticked, or post something extra in the email. Go crazy.

4 Responses to Add Checkboxes to the AJAX Contact Form

  1. Chris

    Does this work the same if using a drop down select list in the form? Do you have to ad anything to the js for a select input?
    Thanks

    • Mike

      See the current code for select box handling.

  2. erez lieberman

    i see this eror:
    Warning: mail() [function.mail]: SMTP server response: 451 See http://pobox.com/~djb/docs/smtplf.html. in D:\Hosting\7080779\html\smartWin\contact.php on line 87
    ERROR!
    when i try to send mail.
    my host is in go dady.
    what is the problem?
    thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>