I have this form page: http://webauthorsgroup.com/raa/contacform/index3.html which is producing an "empty field(s)" error, yet I can't pinpoint where this error is occurring.
I am using the following php form:
<?php
if($_POST)
{
$to_Email = "email@email.com"; //Replace with recipient email address
$subject = 'Web Form'; //Subject line for emails
//check if its an ajax request, exit if not
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
//exit script outputting json data
$output = json_encode(
array(
'type'=>'error',
'text' => 'Request must come from Ajax'
));
die($output);
}
//check $_POST vars are set, exit if any missing
if(empty($_POST["userFirst"]) || empty($_POST["userLast"]) || empty($_POST["userPhone"]) || empty($_POST["userEmail"]))
{
$output = json_encode(array('type'=>'error', 'text' => 'Input fields are empty!'));
die($output);
}
//Sanitize input data using PHP filter_var().
$user_First = filter_var($_POST["userFirst"], FILTER_SANITIZE_STRING);
$user_Last = filter_var($_POST["userLast"], FILTER_SANITIZE_STRING);
$user_Company = filter_var($_POST["userCompany"], FILTER_SANITIZE_EMAIL);
$user_Address = filter_var($_POST["user_Address"], FILTER_SANITIZE_STRING);
$user_City = filter_var($_POST["user_City"], FILTER_SANITIZE_STRING);
$user_State = filter_var($_POST["user_State"], FILTER_SANITIZE_STRING);
$user_Zip = filter_var($_POST["user_Zip"], FILTER_SANITIZE_STRING);
$user_Phone = filter_var($_POST["user_Phone"], FILTER_SANITIZE_STRING);
$user_Email = filter_var($_POST["user_Email"], FILTER_SANITIZE_STRING);
$user_Cell = filter_var($_POST["user_Cell"], FILTER_SANITIZE_STRING);
$user_Type = filter_var($_POST["user_Type"], FILTER_SANITIZE_STRING);
$user_Carrier = filter_var($_POST["user_Carrier"], FILTER_SANITIZE_STRING);
$user_cs_experience = filter_var($_POST["user_cs_experience"], FILTER_SANITIZE_STRING);
$user_contractor_experience = filter_var($_POST["user_contractor_experience"], FILTER_SANITIZE_STRING);
$user_delivery_areas = filter_var($_POST["user_delivery_areas"], FILTER_SANITIZE_STRING);
$user_yr_make_model = filter_var($_POST["user_yr_make_model"], FILTER_SANITIZE_STRING);
$user_ref1_company = filter_var($_POST["user_ref1_company"], FILTER_SANITIZE_STRING);
$user_ref1_contact = filter_var($_POST["user_ref1_contact"], FILTER_SANITIZE_STRING);
$user_ref1_address = filter_var($_POST["user_ref1_address"], FILTER_SANITIZE_STRING);
$user_ref1_city = filter_var($_POST["user_ref1_city"], FILTER_SANITIZE_STRING);
$user_ref1_state = filter_var($_POST["user_ref1_state"], FILTER_SANITIZE_STRING);
$user_ref1_zip = filter_var($_POST["user_ref1_zip"], FILTER_SANITIZE_STRING);
$user_ref2_company = filter_var($_POST["user_ref2_company"], FILTER_SANITIZE_STRING);
$user_ref2_contact = filter_var($_POST["user_ref2_contact"], FILTER_SANITIZE_STRING);
$user_ref2_address = filter_var($_POST["user_ref2_address"], FILTER_SANITIZE_STRING);
$user_ref2_city = filter_var($_POST["user_ref2_city"], FILTER_SANITIZE_STRING);
$user_ref2_state = filter_var($_POST["user_ref2_state"], FILTER_SANITIZE_STRING);
$user_ref2_zip = filter_var($_POST["user_ref2_zip"], FILTER_SANITIZE_STRING);
$user_Message = filter_var($_POST["user_Message"], FILTER_SANITIZE_STRING);
//additional php validation
if(strlen($user_First)<3) // If length is less than 4 it will throw an HTTP error.
{
$output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
die($output);
}
if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) //email validation
{
$output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
die($output);
}
if(!is_numeric($user_Phone)) //check entered data is numbers
{
$output = json_encode(array('type'=>'error', 'text' => 'Only numbers allowed in phone field'));
die($output);
}
/*if(strlen($user_Message)<5) //check emtpy message
{
$output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.'));
die($output);
}*/
//proceed with PHP email.
$headers = 'From: '.$user_Email.'' . "
" .
'Reply-To: '.$user_Email.'' . "
" .
'X-Mailer: PHP/' . phpversion();
$content = $user_Message."
".$user_First."
".$user_Last."
".$user_Company."
".$user_Address."
".$user_City."
".$user_State."
".$user_Zip."
".$user_Phone."
".$user_Email."
".$user_Cell."
".$user_Type."
".$user_Carrier."
".$user_cs_experience."
".$user_contractor_experience."
".$user_delivery_areas."
".$user_yr_make_model."
".$user_ref1_company."
"."
".$user_ref1_contact."
".$user_ref1_address."
".$user_ref1_city."
".$user_ref1_state."
".$user_ref1_zip."
".$user_ref2_company."
".$user_ref2_contact."
".$user_ref2_addressl."
".$user_ref2_city."
".$user_ref2_state."
".$user_ref2_zip;
$sentMail = @mail($to_Email, $subject, $content, $headers);
if(!$sentMail)
{
$output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
die($output);
}else{
$output = json_encode(array('type'=>'message', 'text' => 'Thank you '.$user_First .'. We will be in contact with you.'));
die($output);
}
}
?>
You are not consistent with some of your variable naming. Looking at your HTML source you have these variables:
var user_First = $('input[name=First]').val();
var user_Last = $('input[name=Last]').val();
var user_Company = $('input[name=Company]').val();
var user_Address = $('input[name=Address]').val();
var user_City = $('input[name=City]').val();
var user_State = $('input[name=State]').val();
var user_Zip = $('input[name=Zip]').val();
var user_Phone
Looking at your code you are looking for variables without the underscore:
here:
$user_Company = filter_var($_POST["userCompany"], FILTER_SANITIZE_EMAIL);
Also: having both user_First
and userFirst
is pretty confusing, you might want to clean that up for future debugging.