试图在ajax上获得PHP响应

时间:2018-07-31 12:49:53

标签: php jquery ajax

在这里,我尝试使用jQuery更新MSQL表,PHP代码和jQuery脚本都写在同一页面上,代码可以很好地更新我的表,但响应消息未通过AJAX接收,因此它始终显示我的默认消息是“发生某些问题,请重试。”。

HTML

<!-- HTML code to display msg -->
<p class="statusMsg_post"></p>

jQuery

<script>
    $(document).ready(function(){
        $('#btn_publish').click( function() {           
            var obj_post_postID="<?php echo $post_id; ?>";
            var publish_post="publish";

            $.ajax({
                type:'POST',
                // url:'post_view.php',
                data:{
                    "post_status_update": publish_post,
                    "obj_post_status": obj_post_postID,
                },
                success:function(res) {
                    if (res=='ok') {                       
                        $('.statusMsg_post').html('<span style="color:green;">Post has been '+publish_post+' sucessfully</span>');
                    }
                    else {
                        $('.statusMsg_post').html('<span style="color:red;">Some problem occurred, please try again.</span>');
                    }
                }
            });         
        });         
    });
</script>

PHP

// my PHP code to update my table

<?php
    // require_once('../../inc/db-connect.php');

    if (isset($_POST['post_status_update'])) {
        $temp_publish_post = $_POST['post_status_update'];

        echo $bj_post_postID = $_POST['obj_post_status'];

        $obj_post_status_query = "UPDATE `objection_report` SET `obj_status` = '$temp_publish_post' WHERE `objection_report`.`obj_post_id` = $bj_post_postID;";

        $obj_post_status_query .= "UPDATE `post` SET `status` = '$temp_publish_post' WHERE `post`.`post_id` = $bj_post_postID ;";

        if (mysqli_multi_query($con, $obj_post_status_query)) {
            echo "ok";      
            die;
        }
    }
?>

以上所有代码都在同一页面上

1 个答案:

答案 0 :(得分:0)

脚本在ajax调用中返回HTML和JS。如果您对HTML进行检查,则不会:

send