dongwo7858 2015-01-24 00:08 采纳率: 100%
浏览 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条)

报告相同问题?