为什么这个AJAX调用正确的php脚本没有返回任何东西?

I posted a similar question earlier, just I did not realize that the code I put had an obvious bug in it (not supplying the data) which I new existed. Now I am redoing it with that because I am still having the problem.

I have this AJAX:

function sendMail(){
            $.ajax({
                type: "POST",
                url: "../prestige-auto/php/sendMail.php",
                data: {"message" : $('#contactMessage').html(),
                         "email" : $('#contactEmail').val(),
                         "name" : $('#contactName').val()},
                success: function(result){
                    if (result == 'success') {
                        alert(result);
                    } else {
                        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.
 
 $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?

Don't use array_map('trim', $_POST) http://www.php.net/manual/en/function.trim.php#96246

check ajax response first to see if there is any error occurs.

You didn't exit after echoing out the 'success' - PHP hasn't stopped processing yet, so it just continues to the next line, and renders out echo($message.' '.$email.' '.$name); as well.

This means your JS test is comparing success with success$message $email $name, which is always false.

If you want to stop processing, you have two choices: either exit or die (don't do this), or wrap the rest of the code in an else tag, like this:

if ($message && $email && $name){

    $body = "From: $name.
 
 $message";

    mail(/*some email*/, 'Website Contact Form Submission', $body, "From: $email");

    echo ('success');

} else {

    echo($message.' '.$email.' '.$name);

}

Also, make sure you don't end your PHP scripts with ?> ! This almost always leads to problems later where the PHP script renders out some unwanted white space at the end, which would also fail.