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

报告相同问题?

悬赏问题

  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析