I have this AJAX:
function sendMail () {
$.post('php/sendMail.php', function(result){
alert(result);
})
}
$('#submitContact').click(function(){
sendMail();
})
and this PHP:
<?php
$trimmed = array_map('trim', $_POST);
$message = $trimmed['message'];
$email = $trimmed['email'];
$name = $trimmed['name'];
if (empty($message)) {
$message = FALSE;
}
if (empty($email)) {
$message = FALSE;
}
if (empty($name)) {
$message = FALSE;
}
if ($message && $email && $name){
$body = "From: $name.\n \n $message";
mail(/*some email*/, 'Website Contact Form Submission', $body, "From: $email");
echo ('success');
}
echo($message.' '.$email.' '.$name);
?>
All the html elements defined exist. When I run it all that returns is a blank alert box (meaning that the PHP script did not print out success). Any idea why it is not sending the mail?
You don’t appear to be sending any data with your
POST.If you’re trying to submit a form, try this:
Edit: As Tomalak rightly points out in the comments, you’ll need to specify which form you are serializing. I’d normally give the form an
idand use that:-$('#myform').serialize()