php - 我想将 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'";
解决方案
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>
';
}
推荐阅读
- google-cloud-platform - 如果变量(地图)中包含指定的字符串,Terraform 需要跳过/忽略资源创建
- android - 将图像从 android 上传到 PHP 服务器 - 如何在“net.gotev:uploadservice:4.6.0”中使用 MultipartUploadRequest - 我有一个 version2.1 的代码
- excel - 过滤 sheet1 上 table1 中的数据并将特定列复制到 sheet2 上的 table2
- c# - .NET 中的反射
- flutter - 如何制作像速卖通一样的类别屏幕?
- flutter-getx - 如何使 getx 颤动以移动到下一个中间件
- java - Java 休眠/持久性实体可以具有相关实体 ID 以及相关实体吗?
- python - 如何使用 boto3 对 dynamodb 中的数据进行排序并获得最大值的结果?
- excel - Excel VBA中是否有办法在创建数组时使用两个字符函数
- php - Docker 容器 - 从 PHP 脚本写入 CSV 添加权限