【问题标题】:Clearing notification onclick/toggle()清除通知 onclick/toggle()
【发布时间】:2014-09-01 12:35:37
【问题描述】:

所以我设置了一个通知系统,除了我要清除通知外,它一切正常。当 div 打开时它会清除,但如果我在没有打开 div 的情况下刷新页面,它也会清除,我不希望在用户打开通知 div 之前清除通知。我该怎么做呢?

任何可以为我指明正确方向的帮助或人将不胜感激

谢谢

我一直在用这行代码清除通知

<?php user_core::clear_notifications($user1_id); ?>

这个代码是OnClick toggle()

<div class="alert_header_item_container">
    <a onclick="toggle('alert_dropdown');">
        <div class="alert_header_item" id="alerts"></div>
        <?
        $sql  = "SELECT * FROM notifications WHERE notification_targetuser=".$user1_id." AND notification_status = '1'" ;
        $chant= mysqli_query($mysqli,$sql);
        $num = mysqli_num_rows($chant);
        if($num==1) {
            echo '<div class="alert_header_item_new" id="alerts"></div>';
        } else {
            echo "";
        }
        ?>
    </a>

【问题讨论】:

  • PHP 是服务器端,在 javascript(客户端)之前运行。他们不是爱情伴侣。你想要的是发送一个ajax请求说"Hey I've read this notification/clicked this div so don't show them anymore."因为现在你的PHP正在加载并说yeah, you've read it, because I ran before your onclick event was ever triggered javascript mwuahah。 (请原谅我的悲伤幽默,咖啡还不够。
  • 这完全有道理。我会给它一个爆炸,看看结果。谢谢。
  • 很高兴!如果您需要更多帮助,请告诉我!
  • 我不知道该怎么做 ajax
  • 等一下,我现在给你扔一些伪代码。

标签: javascript php mysqli notifications


【解决方案1】:

您遇到的问题是this:

PHP 不加载或执行 Javascript。这是一个顺序 执行:

  1. 用户从您的 Web 服务器请求数据。
  2. 您的 Web 服务器执行您的 PHP 脚本,该脚本输出一些 HTML/Javascript。
  3. Web 服务器将 HTML/Javascript 发送到用户的浏览器。
  4. 用户的浏览器呈现 HTML 并执行 Javascript。所以是的,PHP 将 在执行 Javascript 之前完成执行。

基本上你的PHP 在你切换onclick 事件之前就完成了。


你真正想做的是这样的:

像这样构建你的HTML

<div class="alert_header_item_container">
    <a id="read-notifications">
        <div class="alert_header_item" id="alerts"></div>
    </a>
</div>

现在我们将运行一个 ajax 请求(当文档加载时)来获取通知

$(document).ready(function(){
    $.ajax({
        type: 'post',
        url: 'notifications.php',
        data: {id: user_id},
    }).success(function(data){
        $('alerts').html(data);
    });
});

这会调用notifications.php,您将在其中运行php/sql 来获取通知:

<?php
$user1_id = $_POST['id']; // then sanitize as needed
$sql = "SELECT * FROM notifications WHERE notification_targetuser=" . $user1_id . " AND notification_status = '1'";
$chant = mysqli_query($mysqli, $sql);
$num = mysqli_num_rows($chant);
if ($num == 1) {
    while($row = mysqli_fetch_assoc()){
        // make notifications markup
    }
   echo MARKUP_FROM_ABOVE
}
?>

至于onclick 事件,你会做这样的事情(到一个单独的 php 文件,它将更新通知以将它们设置为 read(可能是 @987654332 @ ?)):

$(document).on('click', 'a#read-notifications', function(e) {
    $.ajax({
        type: 'post',
        url: 'readnotifications.php',
        data: {id: user_id},
    }).success(function(data) {
        // do what you need to do as notifications were read.
    });
});

readnotifications.php 上,您将运行sql query,它将$_POST['id']用户ID)的通知更新为0(阅读)。

【讨论】:

  • @Dave 很好。我很高兴这是你所追求的!我们都必须从某个地方开始 :-)
猜你喜欢
  • 2013-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-26
  • 1970-01-01
  • 1970-01-01
  • 2017-05-14
相关资源
最近更新 更多