首页 > 解决方案 > 我想将 blog_id 发送到通过 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'";

标签: phpajax

解决方案


The problem is that you are not sending the blog_id from ajax request

<?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 - Never trust user input always escape the input before querying the database (check SQL injection for more info)

$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>
    ';
}

推荐阅读