首页 > 解决方案 > php中使用ajax的通知

问题描述

我正在尝试在 php 中实现推送通知,但我不能从那里获得任何价值。

当 expire_date 在一个月内过期时,代码需要增加通知的数量。

我现在有这个:php文件fetch.php

    <?php

    $con = mysqli_connect("localhost","root","");

    if(!$con){
        echo"Not connect to Server";
    }
    if(!mysqli_select_db($con,'newusers')){
        echo 'Database not select';
    }
    $count=0;
    $DataAuditoria=_$POST['DataAuditoria'];
    $dateToday = date("Y-m-d");

    $query = "SELECT auditorias FROM Clientes";
    $result = mysqli_query($con, $query);
    $output = echo '';

if(mysqli_num_rows($result) > 0)
{

while($row = mysqli_fetch_array($result))

{
    $DataAuditoria1=$row["auditorias"];
    if($DataAuditoria1 != "" && $DataAuditoria1 != NULL){
        $DataAuditoria_notificacao = date("Y-m-d", strtotime(date("Y-m-d", strtotime($DataAuditoria1)) . " -1 months"));

            $diasquefaltam= (int)((strtotime($DataAuditoria1) - strtotime($dateToday))/(60*60*24));

            if( $diasquefaltam<=30 && $diasquefaltam>=0){
                $output .= echo "{$row['Nome']}";
                $output.= echo "{$diasquefaltam}";
                $count++;

            }
}
}

else{
    $output .= echo "No Notifications for today";
}


$data = array(
   'notification' => $output,
   'unseen_notification'  => $count,
);

echo json_encode($data);
}
?>

html: 上传数据的表单

    <div class="modal fade" id="AuditoriaAdd" role="dialog">
        <div class="modal-dialog">
        <div class="modal-footer bg-3">
        <form role="form" method="post" id="comment_form" action="Teste_apagar.php">
        <input type="hidden" name="idClienteAuditoria" class="form-control" placeholder="" > 
        <div class="col-sm-12">

            <h5>Enter Subject</h5>

            <input type="date" name="DataAuditoriaAdd" id="DataAuditoriaAdd" class="form-control">

        </div>

            <button type="submit" class="btn btn-success btn-block" name="post" id="post" class="btn btn-info" value="Post">Add</button>


  </form>
  </div>
  </div>
  </div>

脚本:

    $(document).ready(function(){

// updating the view with notifications using ajax

function load_unseen_notification(view = '')

{

 $.ajax({

  url:"fetch.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();

// submit form and get new records

$('#comment_form').on('submit', function(event){
 event.preventDefault();

 if($('#DataAuditoriaAdd').val() != '')

 {

  var form_data = $(this).serialize();

  $.ajax({

   url:"InsertBD.php",
   method:"POST",
   data:form_data,
   success:function(data)

   {

    $('#comment_form')[0].reset();
    load_unseen_notification();

   }

  });

 }

 else

 {
  alert("Both Fields are Required");
 }

});

// load new notifications

$(document).on('click', '.dropdown-toggle', function(){

 $('.count').html('');

 load_unseen_notification('yes');

});

setInterval(function(){

 load_unseen_notification();;

}, 5000);

});

这些似乎都不起作用,任何人都可以帮助我吗?

标签: phphtmlajax

解决方案


  • 在数据库查询中,您将选择表中的所有值。您是否打算在运行此代码的不同时间截断表?否则,我建议您为此特定请求添加一个日期时间字段或一个关键字段,您可以比较并获取通知。
  • 如果在表中找不到任何记录,则会写入您的 else。您需要更改此设置,因为仅当您的表为空时才会返回一个值。
  • 在您的 jquery 函数中,我可以看到要发布的数据在view变量中,而在您的 php 代码中,post 变量是DataAuditoria

请更正这些并让我知道代码是否有效。


推荐阅读