首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在php后台如何响应ajax请求?

在php后台如何响应ajax请求?
EN

Stack Overflow用户
提问于 2020-09-01 18:38:16
回答 1查看 37关注 0票数 1

我试图在客户端获得成功,但我做不到。如果我把

代码语言:javascript
复制
error:function(data){
                console.log(data);
                console.log('error');
            }

ajax中的这段代码请求this捕获了一些东西,但它不应该出错。

我试了那么多东西,但还是找不到解决方案。

这里是我在客户端的ajax请求;

代码语言:javascript
复制
<script>
$(document).on("submit", "#request-form", function(event){ //request-form id li form post edildiğinde
    event.preventDefault();
    var serialized = $(this).serialize();
    if(serialized.indexOf('=&') > -1 || serialized.substr(serialized.length - 1) == '='){ //formda boş yer var ise
        alert("Fill in all fields");
    }else{
        $.ajax({
            url: "http://127.0.0.1/rent_website/mail-sender/mail.php", //"https://stanstedcab.co.uk/project/mail-sender/mail.php", 
            type: "POST",             
            data: serialized,
            dataType: "json",
            function(data, status) {
                console.log('function works');
                if (data.success) {
                    console.log(data);
                    console.log('Başarılı');
                } else {
                    console.log('else', data)
                }
            },
        });
    }
});
</script>

和这里的后端;

代码语言:javascript
复制
<?php
$response = array();

if ($_POST){
    if(isset($_POST["your-pickup"]) && isset($_POST["your-drop"]) && isset($_POST["Vehicle"]) && 
            isset($_POST["meeting-time"]) && isset($_POST["your-name"]) && isset($_POST["your-phone"]) && 
            isset($_POST["your-email"]) && isset($_POST["your-message"])) {

        //here some mail settings

        if($mail->Send()){
            $message = "Email sent";
            $response["success"] = true;
            $response["message"] = $message;
            echo json_encode($response);
            return $response;
        } else {
            $response = array('result' => 'Email couldn\'t sent', 'success' => false);
            echo json_encode($response);
            return $response;
        }
    }else{
        $response = array('result' => 'Fill all fields.', 'success' => false);
        echo json_encode($response);
        return $response;
    }
}
?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-01 18:45:49

我试着用下面示例中所示的方式编写Javascript:https://api.jquery.com/jquery.ajax/

代码语言:javascript
复制
<script>
$(document).on("submit", "#request-form", function(event){ //request-form id li form post edildiğinde
    event.preventDefault();
    var serialized = $(this).serialize();
    if(serialized.indexOf('=&') > -1 || serialized.substr(serialized.length - 1) == '='){ //formda boş yer var ise
        alert("Fill in all fields");
    }else{
        $.ajax({
            url: "http://127.0.0.1/rent_website/mail-sender/mail.php", //"https://stanstedcab.co.uk/project/mail-sender/mail.php", 
            type: "POST",             
            data: serialized,
            dataType: "json"
       }).done(function(data) {
                console.log('function works');
                if (data.success) {
                    console.log(data);
                    console.log('Başarılı');
                } else {
                    console.log('else', data)
                }
        });
    }
});
</script>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63686030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档