I have a contact form field that uses AJAX to submit the form, and PHP.
I have a small problem. I keep receiving an error that states,"the request was successful, but the form was not filled out correctly.
" This applies to the 'email
' field. The form does not get submitted, and a user has to refill out the form.
Somehow, any email address entered is not valid.
Does anyone know why that could be?
/* Email Variables */
$emailSubject = 'You have received an inquiry from your website';
$webMaster = '[email protected]';
/* Data Variables */
$first = Trim(stripslashes($_POST['First']));
$last = Trim(stripslashes($_POST['Last']));
$email = Trim(stripslashes($_POST['E-Mail']));
$message = Trim(stripslashes($_POST['Message']));
$tempt = $_POST['tempt'];
$tempt2 = $_POST['tempt2'];
if ($tempt == 'http://' && empty($tempt2)) {
$error_message = '';
$reg_exp = "/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/";
if(!preg_match($reg_exp, $email)) {
$error_message .= "<p>A valid email address is required.</p>";
}
if (empty($first)) {
$error_message .= "<p>Please provide your first name.</p>";
}
if (empty($last)) {
$error_message .= "<p>Please provide your last name.</p>";
}
if (empty($message)) {
$error_message .= "<p>Comments box cannot be left empty.</p>";
}
if (!empty($error_message)) {
$return['error'] = true;
$return['msg'] = "<h3> The request was successful, but the form is not filled out correctly.</h3>".$error_message;
echo json_encode($return);
exit();
} else {
$return['error'] = false;
$return['msg'] = "<p>Thank you for contacting us. We definitely value your feedback," .$first .".</p>";
echo json_encode($return);
}
} else {
$return['error'] = true;
$return['msg'] = "<h3>There was a problem while sending this form. Try it again.</h3>";
echo json_encode($return);
}
HTML
<form class="contact-me" action="mail/mailform.php" method="post" name="contact-me">
<div id="response"><!-----------------CONTAINS FORM ERROR MESSAGE--------------></div>
<input name="First" class="required" title="Your First Name is Required" id="first" autofocus placeholder="First Name" type="text" maxlength="15"></input><br>
<input name="Last" title="Your Last Name is Required" id="last" placeholder="Last Name" type="text" maxlength="15"></input><br>
<input name="E-mail" title="E-Mail Address is Required" id="email" placeholder="Email Address" type="email" maxlength="50"></input><br>
<textarea name="Message" title="Comments are Required" placeholder="Comments" rows="1" cols="50" id="message" onKeyDown="textCounter(this.form.comments,this.form.countDisplay);"onKeyUp="textCounter(this.form.message,this.form.countDisplay);"></textarea><br>
<input type="hidden" name="tempt" id="tempt" value="http://" />
<input type="hidden" name="tempt2" id="tempt2" class="clear" value="" />
<input name="countDisplay" type="text" id="countdisplay" value="2000" size="4" maxlength="4" readonly></input><br>
<button class="button" ontouchstart="('touchstart');">SEND</button>
</form>