我想将blog_id发送到通过Ajax调用的页面。我怎样才能做到这一点?

时间:2019-02-18 06:09:22

标签: php ajax

$Blog_id = $_REQUEST['blog'];这是我通过$Blog_id收到的$_REQUEST。现在,我想将此$Blog_id发送到另一个页面,以通过Ajax进行查询。

这是Commenting System的简单形式

<form class="leave-comment" method="post" id="form1">
    h4 class="m-text25 p-b-14">
    Leave a Comment
    </h4>                           
    <div class="bo12 of-hidden size19 m-b-20">
        <input class="sizefull s-text7 p-l-18 p-r-18 user-name" type="text" name="name" id="name" placeholder="Name *">
    </div>
    <textarea class="dis-block s-text7 size18 bo12 p-l-18 p-r-18 p-t-13 m-b-20" name="comment" placeholder="Your Comment Here..." id="comment"></textarea>
    <div class="w-size24">
        <!-- Button -->
        <button class="flex-c-m size1 bg1 bo-rad-20 hov1 s-text1 trans-0-4 posted" id="post_comment" onclick='load_comment()'>
            Post Comment
        </button>
    </div>
</form>

这是load_comment()函数,我用于将请求发送到comments.php页面。

function load_comment() {
        $.ajax({
            url: "Ajax/comments.php",
            method: "POST",
            success: function(data) {
                $('#records_content').html(data);
            },

        });
    }

这是comments.php页面,我想在其中接收$Blog_id

$displayquery = "SELECT * FROM `comments` where blog_id = '$Blog_id'"; 
    $result = mysqli_query($conn,$displayquery);
    while ($row = mysqli_fetch_array($result)) {
        $output .='
        <div class="col-sm-10">
                <div class="col-sm-1">
                    <img src="admin/pages/Ajax/images/blog/3.png" class="img-circle img-responsive" width="40" height="40" alt="">
                </div>
                <div class="col-sm-8"  style="margin-left:5px; margin-top:5px;">
                    <b>'.$row['user_name'].'</b>
                    <small style="margin-left:10px;">'.$formatted_date = date('j M Y', strtotime($row['comment_date'])).'</small>
                    <p>'.$row['user_comment'].'</p>
                    <br><br><br>
                </div>                        
        </div>                        
    ';  
}

但是在查询中,它给了我一个错误undefined variable $Blog_id

 $displayquery = "SELECT * FROM `comments` where blog_id = '$Blog_id'";

3 个答案:

答案 0 :(得分:1)

问题是您没有从ajax请求发送blog_id

<?php $Blog_id = $_REQUEST['blog']; ?>
function load_comment() {
  var blog_id = "<?php echo $Blog_id; ?>";
  $.ajax({
      url: 'Ajax/comments.php',
      dataType: 'json',
      method: 'post',
      data: {'blog_id':blog_id},
      success: function( data, textStatus, jQxhr ){
          console.log(data);
      },
      error: function( jqXhr, textStatus, errorThrown ){
          console.log( errorThrown );
      }
  });
}

comments.php -从不信任用户输入,总是在查询数据库之前转义输入(请查看SQL注入以获取更多信息)

$Blog_id = mysqli_escape_string(htmlentities(trim($_POST['blog_id'])));
$displayquery = "SELECT * FROM `comments` where blog_id = '$Blog_id'";
    $result = mysqli_query($conn,$displayquery);
    while ($row = mysqli_fetch_array($result)) {
        $output .='
        <div class="col-sm-10">
                <div class="col-sm-1">
                    <img src="admin/pages/Ajax/images/blog/3.png" class="img-circle img-responsive" width="40" height="40" alt="">
                </div>
                <div class="col-sm-8"  style="margin-left:5px; margin-top:5px;">
                    <b>'.$row['user_name'].'</b>
                    <small style="margin-left:10px;">'.$formatted_date = date('j M Y', strtotime($row['comment_date'])).'</small>
                    <p>'.$row['user_comment'].'</p>
                    <br><br><br>
                </div>
        </div>
    ';
}

答案 1 :(得分:0)

$displayquery = "SELECT * FROM `comments` where blog_id = '".$Blog_id."'";

您需要将变量连接到查询中。

答案 2 :(得分:0)

您没有在ajax请求参数中传递博客ID,因此您需要在ajax中传递博客ID。

function load_comment() {
        var blogid= "<?php echo $Blog_id; ?>";
        $.ajax({
            url: "Ajax/comments.php",
            method: "POST",
            data: {blog_id: blogid}, // pass here your blog id 
            success: function(data) {
                $('#records_content').html(data);
            },

        });
    }

并在您的comment.php中获取它

$Blog_id=$_POST['blog_id'];