Question

500 error sending AJAX/PHP form with LAMP on Ubuntu

  • Posted August 14, 2014

I’m trying to send a form using AJAX and a php script but I keep getting a 500 error. Dev tools takes me to this line in jQuery -

xhr.send( options.hasContent && options.data || null );

I’m seeing that this could be a cross-domain request issue but I’m not seeing a reason why, any ideas?

$(function() {
  $(".button").click(function() {

    var message = $("textarea#message").val();
    var email = $("input#email").val();   
    var name = $("input#name").val();
    var dataString = 'name='+ name + '&email=' + email + '&message=' + message;

    $.ajax({
      type: 'POST',
      url: "contact.php",
      data: dataString,
      success: function() {
        $('#form').html("<div id='thanks-message'></div>");
        $('#thanks-message').html("<h2>Thank You!</h2>")
        .append("<p>I'll be in touch soon.</p>")
        .hide()
        .fadeIn(1500);
    }
});
    return false;
});
<?php

$to_email = "hello@email.com";

$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];

$subject = "[Contact Form] " . $_POST["subject"];


function isValidEmail($email) {
    return strpos($email, "@") !== false;
}

if($name != "" && $email != "" && $message != "" && isValidEmail($email)) {

	$full_message = "Name : ". $name . "\n";
	$full_message .= "Email : ". $email . "\n";
	$full_message .= "Message :\n\n". $message . "\n"

	mail($to_email, $subject, $full_message);
	header("Location: contact.html");
    exit();
}

die("Your data is invalid.");

?>
Subscribe
Share

Are there any errors in Apache’s error log? What’s the output of:

sudo tail -30 /var/log/apache2/error.log

Are there any errors in Apache’s error log? What’s the output of:

sudo tail -30 /var/log/apache2/error.log

Thank you that helped me find a bug in my php file :)

Thank you that helped me find a bug in my php file :)


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

it outputs

PHP Notice: Undefined index: - for the name, email, message

PHP Parse error: syntax error, unexpected ‘mail’ (T_STRING)