dongluedeng1524 2018-05-16 14:27
浏览 76
已采纳

使用ajax在php中的通知

I'm trying to implement a push notification in php, but i can´t have any value from there.

The code needs to increase the number of notifications when the expire_date is within a month to expire.

i have this for now: php file 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: form to upload data

    <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>

script:

    $(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);

});

none of this seems to work, can anyone help me?

  • 写回答

1条回答 默认 最新

  • dqwyghl0649 2018-05-16 14:59
    关注
    • In the database query, you are selecting all the values in the table. Are you planning to truncate the table at different times of running this code? Else I would suggest that you add a datetime field or a key field for this specific request which you can compare and get the notifications.
    • Your else is written in case no records are found in the table. You would need to change this as this would return a value only if your table is empty.
    • In your jquery function, I can see the data being posted is in the view variable while in your php code, the post variable is DataAuditoria

    Please correct these and let me know if the code works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题