...

View Full Version : Form submission not sending correct response?



bos.the.golfer
01-30-2012, 06:01 PM
High I am using javascript file to validate my form fields and return a response, but I keep receiving my error message instead of my success message. I was wondering if there is an error in my php script? Any help is greatly appreciated.:)



<?php header('Content-type: application/json; charset=utf-8');
sleep(4);

$name = trim(stripslashes(htmlspecialchars($_POST['name'])));
$phone = trim(stripslashes(htmlspecialchars($_POST['phone'])));
$email = trim(stripslashes(htmlspecialchars($_POST['email'])));
$details = trim(stripslashes(htmlspecialchars($_POST['details'])));
$humancheck = $_POST['humancheck'];
$honeypot = $_POST['honeypot'];


if ($honeypot == 'http://' && empty($humancheck)) {

//Validate data and return success or error message
$error_message = '';
$name_check_exp = "/^[A-Za-z ]{3,20}$/";
$phone_check_exp = "/^([1]-)?[0-9]{3}-?[0-9]{3}-?[0-9]{4}$/i";
$email_check_exp = "/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,4}$/";

if (!preg_match($name_check_exp, $name)) {

$error_message .= "<p>Your name is required.</p>";
}
if (!preg_match($phone_check_exp, $phone)) {

$error_message .= "<p>A valid phone number is required.</p>";
}
if (!preg_match($email_check_exp, $email)) {

$error_message .= "<p>A valid email address is required.</p>";
}
if (!empty($error_message)) {
$return['error'] = true;
$return['msg'] = "<h3>Oops! The request was successful but your form is not filled out correctly.</h3>".$error_message;
echo json_encode($return);
exit();

} else {

//send to an email


$emailSubject = 'Bookings Form';
$webMaster = 'bookings@roustaboutblues.com';

$body="
<br><hr><br>
Name: $name <br>
Phone: $phone <br>
Email: $email <br>
I am: $type <br>
Need help with financing: $financing <br>
First home: $first_time <br>
Message: $message
";


$headers .= "Content-type: text/html\r\n";
$headers .= "From: $email\r\n";

//send email and return to user
if(mail($webMaster, $emailSubject, $body, $headers)) {

$return['error'] = false;
$return['msg'] = "<p>Message sent successfully. Thank you for your interest " .$name .".</p>";
echo json_encode($return);

} else {

$return['error'] = true;
$return['msg'] = "<h3>Oops! There was a problem with your submission. Please try again.</h3>";
echo json_encode($return);
}
}
}

?>

BluePanther
01-30-2012, 11:03 PM
High I am using javascript file to validate my form fields and return a response, but I keep receiving my error message instead of my success message. I was wondering if there is an error in my php script? Any help is greatly appreciated.:)



<?php header('Content-type: application/json; charset=utf-8');
sleep(4);

$name = trim(stripslashes(htmlspecialchars($_POST['name'])));
$phone = trim(stripslashes(htmlspecialchars($_POST['phone'])));
$email = trim(stripslashes(htmlspecialchars($_POST['email'])));
$details = trim(stripslashes(htmlspecialchars($_POST['details'])));
$humancheck = $_POST['humancheck'];
$honeypot = $_POST['honeypot'];


if ($honeypot == 'http://' && empty($humancheck)) {

//Validate data and return success or error message
$error_message = '';
$name_check_exp = "/^[A-Za-z ]{3,20}$/";
$phone_check_exp = "/^([1]-)?[0-9]{3}-?[0-9]{3}-?[0-9]{4}$/i";
$email_check_exp = "/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,4}$/";

if (!preg_match($name_check_exp, $name)) {

$error_message .= "<p>Your name is required.</p>";
}
if (!preg_match($phone_check_exp, $phone)) {

$error_message .= "<p>A valid phone number is required.</p>";
}
if (!preg_match($email_check_exp, $email)) {

$error_message .= "<p>A valid email address is required.</p>";
}
if (!empty($error_message)) {
$return['error'] = true;
$return['msg'] = "<h3>Oops! The request was successful but your form is not filled out correctly.</h3>".$error_message;
echo json_encode($return);
exit();

} else {

//send to an email


$emailSubject = 'Bookings Form';
$webMaster = 'bookings@roustaboutblues.com';

$body="
<br><hr><br>
Name: $name <br>
Phone: $phone <br>
Email: $email <br>
I am: $type <br>
Need help with financing: $financing <br>
First home: $first_time <br>
Message: $message
";


$headers .= "Content-type: text/html\r\n";
$headers .= "From: $email\r\n";

//send email and return to user
if(mail($webMaster, $emailSubject, $body, $headers)) {

$return['error'] = false;
$return['msg'] = "<p>Message sent successfully. Thank you for your interest " .$name .".</p>";
echo json_encode($return);

} else {

$return['error'] = true;
$return['msg'] = "<h3>Oops! There was a problem with your submission. Please try again.</h3>";
echo json_encode($return);
}
}
}

?>


Which error message are you receiving? "Problem with submission" or "The request was successful but your form is not filled out correctly"?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum