我正在尝试使用未读通知计数通知铃声。我在php的帮助下使用Javascript从mysqli表中获取数据并将其作为JSON类型返回,但我不知道问题出在哪里,我没有收到任何输出。
这里唯一的工作是,当我点击铃铛图标时,它会将我的comment_status从0更改为1.
谢谢!
这是我的代码:
Javascript:
$(document).ready(function(){
function load_unseen_notification(view = '')
{
$.ajax({
url:"./include/fetch_messages.php",
method:"POST",
data:{view:view},
dataType: "json",
success:function(data)
{
$('.dropdown-menu').html(data.notification);
if(data.unseen_notification > 0)
{
$('.count').html(data["unseen_notification"]);
}
}
});
}
load_unseen_notification();
$(document).on('click', '.dropdown-toggle', function(){
$('.count').html('');
load_unseen_notification('yes');
});
});
我的fetch_messages.php文件:
<?php include 'connect.php'; ?>
<?php
//fetch_messages.php;
if(isset($_POST["view"]))
{
echo "string";
if($_POST["view"] != '')
{
$update_query = "UPDATE comments SET comment_status=1 WHERE comment_status=0";
mysqli_query($conn, $update_query);
}
$query = "SELECT * FROM comments ORDER BY id DESC LIMIT 5";
$result = mysqli_query($conn, $query);
$output = '';
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$output .= '
<li>
<a href="#">
<strong>'.$row["user_commented"].'</strong><br />
<small><em>'.$row["comment"].'</em></small>
</a>
</li>
<li class="divider"></li>
';
}
}
else
{
$output .= '<li><a href="#" class="text-bold text-italic">No Notification Found</a></li>';
}
$query_1 = "SELECT * FROM comments WHERE comment_status=1";
$result_1 = mysqli_query($conn, $query_1);
$count = mysqli_num_rows($result_1);
$data = array(
'notification' => $output,
'unseen_notification' => $count
);
echo json_encode($data);
}
?>
我的html输出的位置是:
<ul >
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="label label-pill label-danger count" style="border-radius:10px;"></span> <span class="glyphicon glyphicon-envelope" style="font-size:18px;"></span></a>
<ul class="dropdown-menu">...</ul>
</li>
</ul>
答案 0 :(得分:0)
我发现了,我有一条&#34; echo&#39; string&#39 ;; &#34;我删除它,现在一切都很好!谢谢你的指导!