View Full Version : PHP not returning my Success message.

01-26-2012, 08:00 PM
Hi all, I am extremely new to PHP and am having an issue with my message after they submit the form. I receive the email and all the information, but there screen returns the error message instead of the success message. Any help is greatly appreciated. :)



$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);

} else {

//send to an email

$emailSubject = 'Bookings Form';
$webMaster = 'bos.the.golfer@gmail.com';

<strong>Name:</stong> $name <br>
<strong>Phone:</stong> $phone <br>
<strong>Email:</stong> $email <br>
<strong>Details:</stong> $details

$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\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 intrest " .$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);

01-26-2012, 08:20 PM
Doesn't look like a problem from this end. Disable your javascript, does the encoded result reflect that of success or error messages? From the looks of your conditional structure, it is impossible for it to both mail and to trigger an error.

01-26-2012, 10:18 PM
Thank you for your response. How could I fix it so that it would mail it and send a response? This is only the second time ever trying to script php:)

01-26-2012, 10:21 PM
First you have to determine what the issue is. Like I said, it looks fine from what I see, the conditions are correct (minus a message which indicates a possible mail failure itself, but that is really up to you if you want to display that). So my immediate suspicion is that this is a caching issue with your javascript.
As of right now, it sends an email AND it provides a successful result. Which is why I'm leaning to an issue caused by your javascript, not the PHP code.

01-26-2012, 10:44 PM
Thank you for the response I will take a look at the javascript as it has to be one of the last lines there and that is the message that pops up. But this is my first try at javascript form validation. By chance do you know javascript as well as you do php?:)

01-27-2012, 12:13 AM
No, my javascript is from when the DOM was first adopted by browsers. So like, 8 years ago :/
I'd just disable JS in my browser and see what PHP does. If it still says failed and you still get an email, something is definitely wrong with PHP.

01-27-2012, 03:08 AM
what is the exact error message showing on screen?

01-27-2012, 04:40 AM
Its the error message from my js script. I will paste it below:

function submitForm(formData) {

type: 'POST',
url: 'bookings.php',
data: formData,
dataType: 'json',
cache: false,
timeout: 7000,
success: function(data) {

$('form #response').removeClass().addClass((data.error === true) ? 'error' : 'success')

if ($('form #response').hasClass('success')) {

setTimeout("$('form #response').fadeOut('fast')", 5000);

error: function(XMLHttpRequest, textStatus, errorThrown) {

$('form #response').removeClass().addClass('error')
.html('<strong>There was an error. Please use the above information and contact us directly.</strong>').fadeIn('fast');
complete: function(XMLHttpRequest, status) {