从php文件中接收来自jquery Ajax的空响应

时间:2018-05-05 12:09:14

标签: php jquery ajax

我正在PHP jquery和Ajax中创建一个评论回复系统。到目前为止,我设法用php和jquery ajax进行评论并回复评论动作。当我回复评论时,我从jquery ajax收到回复评论的数量,然后我在屏幕上打印这个号码。现在我想要做的是在提交后将回复打印到屏幕上。我制作了jquery ajax函数并编写了我的php脚本。然后我回应从PHP格式化的$输出回到ajax。问题是我收到空白响应然而我直接测试了php文件并且它工作正常并输出到屏幕$ output变量。请帮忙。这是我的ajax部分,负责输出回复:

  <script type="text/javascript">
    load_replies();
    function load_replies() {
     $.ajax({
     url: "widgets/board_reply_fetch.php?comment_id=<?php echo 
$board_comment_id_number;?>",
            method: "POST",
            success: function(data){
                $("#reply_comment").html(data);
                console.log(data);
            }
        });
    }
</script>

这是我的php文件:

<?php 
require_once '../includes/session.php';
require_once '../includes/functions.php';
require_once '../includes/validation_functions.php';

if (isset($_GET["comment_id"])) { 
$comment_id = (int)$_GET["comment_id"];

$reply_data = find_board_replies_by_comment_id($comment_id);

$output = "";
$output_array = array();
while ($reply_assoc = mysqli_fetch_assoc($reply_data)) {
    $reply_comment_id = $reply_assoc['comment_id'];
    $reply_board_id = $reply_assoc['board_id'];
    $reply_user_id = $reply_assoc['user_id'];
    $reply_text = $reply_assoc['reply'];
    $reply_timestamp = $reply_assoc['reply_timestamp'];

    $reply_user_data = find_user_data_by_id($reply_user_id);


    $profile_image = $reply_user_data['profile_picture']; 
    $profile_image_thumb = "../uploaded_pictures/profile/$reply_user_id/" . $reply_user_id . "small.png";

    if ($profile_image == "") {
        if ($comment_user_data['gender'] == "Male"){
            $user_profile_picture = "../images/ProfilePicMale.png";
        } else {
            $user_profile_picture = "../images/ProfilePicFemale.png";
        }
    } else {
        $user_profile_picture = $profile_image_thumb;
    }

    $full_name = ucfirst(htmlentities($reply_user_data['first_name'])) . " " . ucfirst(htmlentities($reply_user_data['last_name']));
    $time_of_post = time_of_post($reply_timestamp);
    $the_reply_text = nl2br($reply_text);

    $output = "<div class=\"reply_comment_div\">";
    $output .= "<a href=\"profile.php?user_id=$reply_user_id\" class=\"board_comments_div_picture\">";
    $output .= "<img src=\"$user_profile_picture\" width=\"50px\" height=\"50px\" /></a>";
    $output .= "<a href=\"profile.php?user_id=$reply_user_id\" class=\"board_comments_reply_link\">$full_name</a>";
    if ($reply_user_id == $_SESSION['user_id']){ 
        $output .= "<a href=\"edit_comment_board.php?comment_id=$reply_comment_id\" class=\"edit_comment_button_board\">Edit</a>";
        $output .= "<a href=\"widgets/delete_board_comment.php?board_id=$reply_board_id&comment_id=$reply_comment_id\" onclick=\"return confirm('Are you sure you want to delete this admin?')\" class=\"delete_button_status\">Delete</a>";
    }
    $output .= "<div class=\"board_comment_submited_on\">submitted $time_of_post</div>";
    $output .= "<span class=\"comment_content_span\">$the_reply_text</span>";
    $output .= "</div>";

    $output_array[] = $output;
}
foreach ($output_array as $array) {
    echo $array;
}
}

&GT;

2 个答案:

答案 0 :(得分:0)

尝试存储发送到数据对象

中的php页面的数据
  

示例:

<script type="text/javascript">
    load_replies();
    function load_replies() {
    var commment_id = "<?php echo $board_comment_id_number;?>";
    $.ajax({
        url: "widgets/board_reply_fetch.php",
        data:{commentId:commment_id },//commment_id store it here
        method: "GET",
        success: function(data){
            $("#reply_comment").html(data);
            console.log(data);
        }
    });
  }
</script>

答案 1 :(得分:0)

使用POST METHOD:

if (isset($_POST["comment_id"])) { 
$comment_id = (int)$_POST["comment_id"];