im using PHP / AJAX to email out some information from a form. Basic form, AJAX and PHP script below, keep getting 500 error and ive no idea why! I've used this same setup or very similar more often than not, i've tested the "mail" function on the server and thats working just fine.
Any help would be awesome!
Thankyou
// quickquote form send
$(function() {
// Get the form.
var form = $('#quickquote');
// Get the messages div.
var formMessages = $('#form-messages');
// Set up an event listener for the contact form.
$(form).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('alert alert-danger');
$(formMessages).addClass('alert alert-success');
// Set the message text.
$(formMessages).text(response);
// Clear the form.
$('#quickquote')[0].reset();
})
.fail(function(data) {
// Make sure that the formMessages div has the 'error' class.
$(formMessages).removeClass('alert alert-success');
$(formMessages).addClass('alert alert-danger');
// 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.');
}
});
});
});
<?php
// My modifications to mailer script from:
// http://blog.teamtreehouse.com/create-ajax-contact-form
// Added input sanitizing to prevent injection
// Get the form fields and remove whitespace.
$name = $_POST['name'];
$email = $_POST['email'];
$tel = $_POST["phone"];
$address = $_POST["address"];
$length = $_POST["length"];
$width = $_POST["width"];
// Set the recipient email address.
// Update this to your desired email address.
$recipient = "email@email.com";
// Set the email subject.
$subject = "Contact Form Request from $name";
// Build the email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n";
$email_content .= "Telephone: $tel\n";
$email_content .= "Address: $address\n";
$email_content .= "Length: $length\n";
$email_content .= "Width: $width\n\n";
// Build the email headers.
$email_headers = "From: $name <$email>";
// Send the email.
if (mail($recipient, $subject, $email_content, $email_headers)) {
// Set a 200 (okay) response code.
http_response_code(200);
echo "Thank You! Your message has been sent.";
} else {
// Set a 500 (internal server error) response code.
http_response_code(500);
echo "Oops! Something went wrong and we couldn't send your message.";
}
} else {
// Not a POST request, set a 403 (forbidden) response code.
http_response_code(403);
echo "There was a problem with your submission, please try again.";
}