Php/ajax errors with submitting contact form

Hi.

So I bought a template recently and the contact form is not working on my page. I get, “Oops! An error occured and your message could not be sent.”

I went to the author and asked them for help. They do not respond with anything but “it works on our site, I don’t understand why it isnt working. It must be my host, please give your FTP credentials.”

Massively leery of doing that so I guess I figure this out some other way.

Im not even really sure if this is a PHP problem or a js file that needs to run for the form to work.

My site is here: overbuiltbarns.com

This is the js that has the conctact form js as well as other elements.

*                  13. Ajax Contact Form js                               *
     *-------------------------------------------------------------------------*/
		// Get the form.
		var form = $('#ajax-contact');

		// Get the messages div.
		var formMessages = $('#form-messages');

		// Set up an event listener for the contact form.
		$(form).on('submit', function(e) {
		  // Stop the browser from submitting the form.
		  e.preventDefault();

		  // Serialize the form data.
		  var formData = $(form).serialize();

		  // Submit the form using AJAX.
		  $.ajax({
			  type: 'POST',
			  url: $(form).attr('action'),
			  data: formData
		  })
		  .done(function(response) {
			  // Make sure that the formMessages div has the 'success' class.
			  $(formMessages).removeClass('error');
			  $(formMessages).addClass('success');

			  // Set the message text.
			  $(formMessages).text(response);

			  // Clear the form.
			  $('#name').val('');
			  $('#email').val('');
			  $('#subject').val('');
			  $('#message').val('');

		  })
		  .fail(function(data) {
			  // Make sure that the formMessages div has the 'error' class.
			  $(formMessages).removeClass('success');
			  $(formMessages).addClass('error');

			  // Set the message text.
			  if (data.responseText !== '') {
				  $(formMessages).text(data.responseText);
			  } else {
				  $(formMessages).text('Oops! An error occured and your message could not be sent.');
			  }
		  });

		});

and this is my mailer.php file:

<?php

if(!$_POST) exit;

$email = $_POST['email'];


//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
    $error.="Invalid email address entered";
    $errors=1;
}
if($errors==1) echo $error;
else{
    $values = array ('name','email','message','subject');
    $required = array('name','email','message');
   
    $your_email = "[email protected]";
    $email_subject = "webform: ".$_POST['subject'];
    $email_content = "new message:\n";
  
    foreach($values as $key => $value){
      if(in_array($value,$required)){
        if ($key != 'subject' && $key != 'company') {
          if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
        }
        $email_content .= $value.': '.$_POST[$value]."\n";
      }
    }
   
    if(@mail($your_email,$email_subject,$email_content)) {
        echo 'Thank You! Your message has been sent.';
    } else {
        echo 'ERROR!';
    }
}
?>

I followed the instructions to set up the mailer which was to simply edit $your_email = “your email here” and include my email. Which I did.

Could this be host based? or is this crappy code?
Thanks

At a minimum, the .fail() code is incorrect. According to the documentation, there is no responseText property for a failure (responseText is part of a successful response.) There is a statusText property that will probably give the reason for the failure, or the code should use the current calling syntax and use the 2nd parameter - textStatus

Insightful.

I’m not a coder though. What do you recommend as a fix?

My view is, the code is questionable. These two things point to inexperience,

if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
    $error.="Invalid email address entered";
    $errors=1;
}

Should just be,

if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
    $error.="Invalid email address entered";
    $errors=1;
}

And the big one,

if(@mail($your_email,$email_subject,$email_content)) {

The ‘@’ suppresses errors, which you don’t want to do.

1 Like

@astonecipher Those adjustments made it work.

Thank you sooooo much!!!

That means they are using way out of date methods.

eregi()
Warning

This function was DEPRECATED in PHP 5.3.0, and REMOVED in PHP 7.0.0.

Which again points to crappy code by inexperience developers.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service