简体   繁体   English

为什么这个 ajax 调用正确的 php 脚本没有返回任何内容?

[英]Why is this ajax call to the correct php script not returning anything?

I have this AJAX:我有这个 AJAX:

function sendMail () {
        $.post('php/sendMail.php', function(result){
            alert(result);
        })
    }

$('#submitContact').click(function(){
        sendMail();
    })

and this PHP:还有这个 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.定义的所有 html 元素都存在。 When I run it all that returns is a blank alert box (meaning that the PHP script did not print out success).当我运行它时,所有返回的是一个空白警报框(这意味着 PHP 脚本没有打印出成功)。 Any idea why it is not sending the mail?知道为什么它不发送邮件吗?

You don't appear to be sending any data with your POST .您似乎没有使用POST发送任何数据。

If you're trying to submit a form, try this:如果您尝试提交表单,请尝试以下操作:

$.post('php/sendMail.php',$('form').serialize(), function(result){
        alert(result);
    })

Edit: As Tomalak rightly points out in the comments, you'll need to specify which form you are serializing.编辑:正如 Tomalak 在评论中正确指出的那样,您需要指定要序列化的表单。 I'd normally give the form an id and use that:- $('#myform').serialize()我通常会给表单一个id并使用它:- $('#myform').serialize()

You're not sending any data to that URL.您没有向该 URL 发送任何数据。

The reason you are seeing an empty alert box is because $email and $name is empty because you didn't pass any $_POST variable.您看到一个空警报框的原因是因为$email$name是空的,因为您没有传递任何$_POST变量。
And $message = FALSE is blank when you echo it.当你回显它时, $message = FALSE是空白的。
Try changing this line尝试更改此行

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

To

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

And you'll see the value of $message.' '.$email.' '.$name你会看到$message.' '.$email.' '.$name $message.' '.$email.' '.$name $message.' '.$email.' '.$name . $message.' '.$email.' '.$name .

Any idea why it is not sending the mail?知道为什么它不发送邮件吗?

Put simply, you didn't ask it to .简而言之,你没有要求它


The syntax for $.post is: $.post的语法是:

jQuery.post( url, [data,] [success(data, textStatus, jqXHR),] [dataType] ) jQuery.post(url,[数据,][成功(数据,textStatus,jqXHR),][数据类型])

The way jQuery works, you may skip the data argument and still provide the following ones, but in this case you're simply not passing any data for your PHP script to work with. jQuery 的工作方式,您可以跳过data参数并仍然提供以下参数,但在这种情况下,您根本没有为您的 PHP 脚本传递任何数据。

Assuming that #submitContact is a button in the form that you want to submit, you should serialise that form's contents and send that as the POST data:假设#submitContact是您要提交的表单中的一个按钮,您应该序列化该表单的内容并将其作为POST数据发送:

function sendMail($form) {
    $.post('php/sendMail.php', $form.serialize(), function(result) {
        alert(result);
    });
}

var $btn = $('#submitContact');
var $frm = $btn.parents('form');
$btn.click(function() {
    sendMail($frm);
});

The documentation is your friend.文档是你的朋友。 Use it.用它。


( Note: The $ at the start of some of my variables is deliberate; it's a personal choice that jQuery objects have names starting with $ in my code.) 注意:我的一些变量开头的$是故意的;在我的代码中 jQuery 对象的名称以$开头是个人选择。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM