两个或多个ajax调用一个PHP文件

时间:2016-07-06 17:05:35

标签: php jquery ajax

我知道这已被问及我看了其他主题,但无法找到答案。

我有两个ajax调用,指向同一页面。一个删除语言选择,另一个删除教育经验。奇怪的是,PHP页面上的最后一个查询工作(目前是语言)。

编辑: 为什么第一个查询不起作用? (当试图删除教育exp时)

PHP

/*------------------------------------------
[NANY EDUCATION EXP DELETE]
-------------------------------------------*/ 
if(isset($_POST['education_id']) && !empty($_POST['education_id'])){
    $education_id = $_POST['education_id'];
    $stmt = $user_home->runQuery("DELETE FROM user_education WHERE education_id=:education_id");
    $stmt->execute(array(':education_id' => $education_id));
    $response_array['status'] = 'success';
}else{
    $response_array['status'] = 'error';

}

/*------------------------------------------
[NANY LANGUAGE EXP DELETE]
-------------------------------------------*/ 
if(isset($_POST['language_id']) && !empty($_POST['language_id'])){
    $language_id = $_POST['language_id'];
    $stmt = $user_home->runQuery("DELETE FROM user_language WHERE language_id=:language_id");
    $stmt->execute(array(':language_id' => $language_id));
    $response_array['status'] = 'success';
}else{
    $response_array['status'] = 'error';

}

JS

            /*------------------------------------------
            [NANY EDUCATION EXP DELETE]
            -------------------------------------------*/ 
            $(document).on("click", ".deleteEducationEXP", function (e) {
                e.preventDefault();
                var education_id = $(this).attr('id'); 
                $(this).parent().parent().parent().fadeOut(300, function() { $(this).remove(); });

                $.ajax({
                    type: "POST",
                    url:'PHP/deleteData.php',
                    data:"education_id=" + education_id,
                    success:function(data){
                        if(data.status == 'success'){
                            console.log("success"); 
                        }else if(data.status == 'error'){
                            console.log("error"); 
                        }
                    },      
                    error: function(jqXHR, textStatus, errorThrown, data){
                        console.log(jqXHR, textStatus, errorThrown, data);
                    } 
                });
            });

            /*------------------------------------------
            [NANY LANGUAGE EXP DELETE]
            -------------------------------------------*/ 
            $(document).on("click", ".deleteLangugage", function (e) {
                e.preventDefault();
                var language_id = $(this).attr('id'); 
                $(this).parent().parent().fadeOut(300, function() { $(this).remove(); });

                $.ajax({
                    type: "POST",
                    url:'PHP/deleteData.php',
                    data:"language_id=" + language_id,
                    success:function(data){
                        if(data.status == 'success'){
                            console.log("success"); 
                        }else if(data.status == 'error'){
                            console.log("error"); 
                        }
                    },                
                    error: function(jqXHR, textStatus, errorThrown, data){
                        console.log(jqXHR, textStatus, errorThrown, data);
                    }
                });
            });

1 个答案:

答案 0 :(得分:2)

问题是两个else子句都设置了$response_array['status'] = "error";

当他们删除教育时,第一个if块成功,从教育表中删除并设置$response_array['status'] = "success";。然后它转到下一个if块,该块失败并覆盖$reponse_array['status']

使用elseif组合它们。

if(!empty($_POST['education_id'])){
    /*------------------------------------------
    [NANY EDUCATION EXP DELETE]
    -------------------------------------------*/ 
    $education_id = $_POST['education_id'];
    $stmt = $user_home->runQuery("DELETE FROM user_education WHERE education_id=:education_id");
    $stmt->execute(array(':education_id' => $education_id));
    $response_array['status'] = 'success';
}elseif(!empty($_POST['language_id'])){
    /*------------------------------------------
    [NANY LANGUAGE EXP DELETE]
    -------------------------------------------*/ 
    $language_id = $_POST['language_id'];
    $stmt = $user_home->runQuery("DELETE FROM user_language WHERE language_id=:language_id");
    $stmt->execute(array(':language_id' => $language_id));
    $response_array['status'] = 'success';
}else{
    $response_array['status'] = 'error';
}

顺便说一句,您不需要同时使用isset()!empty(),因为empty()会检查它是否先设置。