Hi,
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.
<h1>Contact</h1>
<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 -->
</form>
Heres the PHP:
<?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) {
require_once('phpMailer/class.phpmailer.php');
$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