dongwo7858 2015-01-24 00:08
浏览 86
已采纳

使用php代码设置setInterval

I wanted to make a "friend request" option on my website and I would like if friend requests can be displayed as soon as possible. (even if I don't reload page) So what I've read here I need setInterval and Ajax, I found the code, and it works perfect for the first time, but it doesn't refresh on database changes. Is that intended or am I doing something wrong? Example, I have 2 friend requests, and I get one more friend request while my page is idle, and it doesn't shows up, even tho my javascript is doing the query again, right? I know I shouldn't use mysql_query, and I will fix that as soon as possible, but no way that could be the problem.

$(document).ready(function(){ 
    var ajax_call = function() {
    document.getElementById('js_hack').innerHTML = "<?php 
    $ses_sql3=mysql_query("select * from friend_request where reciver='$login_username'", $connection);
    $num_rows = mysql_num_rows($ses_sql3);
    if($num_rows > 0) { echo $num_rows; }?>";
    };

    var interval = 1000 * 1; // where X is your every X minutes

    setInterval(ajax_call, interval);
});
  • 写回答

2条回答 默认 最新

  • duanjipiao7076 2015-01-24 00:10
    关注

    Place your <?php?> code in a separate file, and call that file.

    I didn't look at the rest of your code to validate that it is "good", thought.

    You can't have serverside code perform on the client side, like that; nor do you want it to.

    Here is an example, and what looks to be a dupilcate question: Ajax time interval call to a php function

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮