php - 通知系统无法正常工作 JSON/PHP

标签 php mysql json ajax notifications

我正在尝试使用未读通知的计数来发出通知铃声。我在 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>

最佳答案

我找到了它,我有一行“echo 'string';”,我将其删除,现在一切都很好!感谢您的指导!

关于php - 通知系统无法正常工作 JSON/PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49936147/

相关文章:

php - 如何在 codeigniter 中放置多个提交按钮

mysql从group by设置多条记录

MySQL Workbench 报告 “is not valid at this position for this server version” 错误

ios - 如何在 iOS Swift 中使用 JSON 数据在表格 View 中显示标题(类型)?

json - fbexternal-a.akamaihd.net/safe_image.php 返回 1x1 图像

php - 使用 AJAX Post 的响应

php - PDO fetch() 是否在失败时抛出异常?

php - 我怎样才能让这个 mysqli 数据库类工作?

php - Laravel 5 递归函数多对多自引用模型

javascript - 反向图像搜索特定网站