Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中将状态和数据返回到html_Php_Email_Post - Fatal编程技术网

在PHP中将状态和数据返回到html

在PHP中将状态和数据返回到html,php,email,post,Php,Email,Post,我正在尝试使用post()从PHP到jquery获取mail()的结果。如何在JQuery中设置post()并在PHP中发送邮件的状态 In.php $status = mail($to, $subject, $msg, $headers); if ($status) { http_response_code(200); echo 'Mail sent!'; } else { http_response_code(500); echo 'Something

我正在尝试使用post()从PHP到jquery获取mail()的结果。如何在JQuery中设置post()并在PHP中发送邮件的状态

In.php

$status = mail($to, $subject, $msg, $headers);
if ($status) {
    http_response_code(200);
    echo 'Mail sent!';
} else {
    http_response_code(500);    
    echo 'Something went wrong!';
}
在JQuery中

$.post('test.php', data, function(data) {
  alert('data: ' + data);
})

我必须从php返回数据吗?

如果您使用from jQuery来处理这个问题,最好使用
.done()
来处理200个响应,使用
.fail()
来处理500个响应

$.post('test.php', data)
  .done(data => { 
    alert('Success: ' + data) // should be "Success: Mail sent!"
  }).fail((jqXHR, textStatus, errorThrown) => {
    alert('Failed: ' + jqXHR.responseText) // should be "Fail: Something went wrong!"
  })

没有更详细的错误信息,很难说。但这是我的go-to-vanilla Ajax请求发送器,它是我前一段时间创建的

class AjaxRequest{
    constructor(){
        this.httpReq = new XMLHttpRequest();
    }
    setup(type, url, func){
        func = func || function() { };

        function callbackFunc(){
            if (this.httpReq.readyState === 4) {
                if (this.httpReq.status === 200) {
                    let data = this.httpReq.response;
                    //console.log("Returned from ajax query: ", data);
                    func(data);
                }
                // Catch errors here
                else {
                    console.log('There was a problem with the request.');
                }
            }
        }
        this.httpReq.open(type, url);
        this.httpReq.setRequestHeader("Content-type", "application/json");
        this.httpReq.onreadystatechange = callbackFunc.bind(this);
    }
    send(params){
        params = params || {};
        params = JSON.stringify(params);
        this.httpReq.send(params);
    }
}
要使用它,请使用请求类型、URL和回调实例化它、setup()

所以对于你的情况

let ajax = new AjaxRequest();
ajax.setup('POST', 'test.php', function(response) {
    console.log(response);
});
ajax.send(); //pass in data that you want to send here

你有什么错误吗?检查您的控制台空
数据可能有错误
,据我所知,您无法发送空数据,这样做没有错误。邮件正常发出。我不能得到警报。哎呀,代码中有警报('data:'+data')。@Gene9y很抱歉,我不理解您的评论,至少它抛出了错误,这意味着它正在响应代码。@Gene9y您是说您收到了警报的“Fail”消息?如果是这种情况,那么在PHP中
mail()
将返回
false
,这意味着failed@Gene9y客户端(JavaScript)代码对您的PHP执行完全没有影响。我的答案中的代码与您的问题中的代码没有什么不同,只是添加了一个错误处理程序。@Gene9y是时候检查一下浏览器的网络控制台了。响应状态代码是什么?是代码中的响应文本“出错了!”还是其他内容?是否有任何PHP错误记录在任何地方?