如何用AJAX告诉PHP在文章下面显示哪些注释
How to tell PHP which comments to show under the article with AJAX?
我正在为我的网站建立一个新闻页面,但我卡住了用ajax显示正确的评论…
commentsLoad.php
<?php
include('config.php');
$newsid = $_GET['newsid'];
$comments=array();
$commentsQuery = "SELECT * FROM comments
where fk_news like ".$newsid;
$result = $conn->query($commentsQuery);
if($result->num_rows>0){
while($row = $result->fetch_assoc()){
$comments[]=array('id' => $row['id'], 'name' => $row['cnick'], 'text' => $row['ctext'], 'date' => $row['cdate']);
}
}
//header('Content-type: application/json');
echo json_encode($comments);
exit;
?>
我不知道如何传递正确的'NEWSID'
网站图片:http://prntscr.com/8nwy8k
我如何将ID传递给SQL查询
$.ajax({
type: 'GET',
url: commentsUrl,
dataType: "json",
data:{newsid:'1'},
success: function(comments){
//console.log(komentarji);
$.each(comments, function(i, komentar){
addComment(komentar);
})
},
error: function(e){
console.log(e);
}
});
所以现在如果我改变data:{newsid:'1 or 2 or 3…'}我得到了我想要的注释,但是我不知道如何把那个ID变成一个变量。
可以使用onClick事件。
解释:
Comment
链接如下所示
<a href="javascript:void(0)" onClick="getComments('<?php echo $YOUR_ARTICLE_ ID?>')">Comments</a>
然后你可以在JQuery代码中有一个函数来传递它到PHP文件。
function getComments(article_id)
{
var artid = article_id;
$.ajax({
type: 'POST',
url: commentsUrl,
dataType: "json",
data:{newsid: artid},
success: function(comments){
$.each(comments, function(i, komentar){
addComment(komentar);
})
},
error: function(e){
console.log(e);
}
});
}
尝试在评论链接中设置onclick功能。
<a href="javascript:void(0)" onclick='myfunction <?php echo newsid ?>'Comment</a>
从链接中获取newsid
<script>
function myfunction(newsid){
$.ajax({
type: 'GET',
url: commentsUrl,
dataType: "json",
data:{newsid:newsid},
success: function(comments){
//console.log(komentarji);
$.each(comments, function(i, komentar){
addComment(komentar);
})
},
error: function(e){
console.log(e);
}
});
}
</script>
从commentsurl页面获取新标题
相关文章:
- 谷歌表格注释时间线显示精确的值
- 未注释img´除非调整浏览器大小、缩放或打开开发工具,否则不会显示
- 如何在JavaScript中显示注释线程层次结构
- 绘图注释未显示在正确的位置
- 在 AngularJS 中显示多个注释
- 用我的ajax代码来显示注释
- 如何添加一个显示/隐藏注释链接不工作与下划线.js
- 如何在图像的特定点显示注释
- 在文本区域添加注释,并使用“编辑”和“删除”按钮显示文本
- 错误:通过ajax(从数据库)显示注释(laravel 5.2)
- 如何用AJAX告诉PHP在文章下面显示哪些注释
- 显示页面的注释,但不允许添加新的注释
- 谷歌图表注释不显示
- 在网页中显示长文本区域注释
- 显示带有Internet Explorer条件注释的javascript警报
- 高亮选择与注释,显示在悬停和可编辑
- 文本突出显示和添加注释功能在epub阅读器ios
- 如何为ReSharper注释以便智能感知显示文档
- 悬停在上面时显示打印线长度的注释
- 在Chrome扩展中使用qTip显示注释