I am trying to add a checkbox to a form on a friends site and I need to make the form validate the checkbox before processing but I cant seem to get my head round it.

Heres the code so far.


            <p>Please fill in the following fields and we'll get back to you as soon as possible.</p>

            <form method="post" action="sendEmail.php">
                <div id="main">
                    <p><label for="name">Name:*</label> <input type="text" name="name" id="name" tabindex="1" /></p>
                    <p><label for="email">Email:*</label> <input type="text" name="email" id="email" tabindex="2" /></p>
                    <p><label for="comments">Comments:*</label> <textarea name="comments" id="comments" cols="12" rows="12" tabindex="3"></textarea></p>

<p>Want our free brochure? <input type="checkbox" name="checkbox" id="checkbox" /></p>

                    <p><input type="submit" name="submit" id="submit" value="Email Us!" tabindex="4" />
                    <p>* - required</p>
                    <ul id="response"><li></li></ul>
                </div><!--end main -->

Heres the PHP:


    $name = trim($_POST['name']);
    $email = $_POST['email'];
    $comments = $_POST['comments'];
    $site_owners_email = '###########'; // Replace this with your own email address
    $site_owners_name = 'sales'; // replace with your name
    if (strlen($name) < 2) {
        $error['name'] = "Please enter your name";  
    if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
        $error['email'] = "Please enter a valid email address"; 
    if (strlen($comments) < 3) {
        $error['comments'] = "Please leave a comment.";
    if (!$error) {
        $mail = new PHPMailer();
        $mail->From = $email;
        $mail->FromName = $name;
        $mail->Subject = "Website Contact Form";
        $mail->AddAddress($site_owners_email, $site_owners_name);
        $mail->Body = $comments;
        // If you want to use receive your email at Gmail then uncomment the below lines and enter your gmail address and password.
        $mail->Mailer = "smtp";
        $mail->Host = "###########"; 
        $mail->SMTPAuth = true; // turn on SMTP authentication
        $mail->Username = "##########"; // SMTP username
        $mail->Password = "########"; // SMTP password
//      $mail->Send();
//      echo "<li class='success'>Thanks " . $name . ". We've received your email. We'll be in touch as soon as we possibly can! </li>";
        if ($mail->Send())

            echo'<script>window.location.href="http://###"; </script>';
    } # end if no error
    else {
        $response = (isset($error['name'])) ? "<li>" . $error['name'] . "</li> \n" : null;
        $response .= (isset($error['email'])) ? "<li>" . $error['email'] . "</li> \n" : null;
        $response .= (isset($error['comments'])) ? "<li>" . $error['comments'] . "</li>" : null;
        echo $response;
    } # end if there was an error sending


The form validates the other fields fine but I need it to also check to see if the checkbox is selected before it sends the information.

Thanks Theo

Recommended Answers

All 4 Replies

Member Avatar

Don't use preg_match() for email, use filter_var(). OK...

$name = trim($_POST['name']);
$email = $_POST['email'];
$comments = $_POST['comments'];
$checkbox = isset($_POST['checkbox']);

The $checkbox now stores boolean (true/false). $_POST['checkbox'] does not exist if checkbox unchecked.

Hi, I have added the new line into my php but it still allows the form to be processed without the checkbox being selected.

i need it to give an error telling the user that they have to check the box in order to proceed.


Member Avatar

Ah sorry misread. You just use js to enable and disable the submit button. However do you want to force somebody to sign up to a brochure? Seems a bit extreme to me. If I was confronted with this sort of choice, I would refuse to sign up out of principle. Just a thought

Member Avatar

With jQuery, you could do this:

    $('#submit').attr('disabled', !$(this).prop( "checked" ));

But make the submit button disabled to begin with:

<input type="submit" name="submit" id="submit" disabled value="Email Us!" tabindex="4" />

But, as I said, this is a bit yuck. Forcing your brochure on somebody just for giving you some feedback.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.