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 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类