donglian1523 2014-05-15 18:37
浏览 31
已采纳

继续更新值。 (ajax - 来自.php文件的民意调查)

I'm trying to modify this http://www.w3schools.com/php/php_ajax_database.asp

I'm just using the HTML part of that ^. My php file only have

<?php
echo rand();
?>

And it works fine! It updates every time I switch something on the drop-down list. But, I want it to run every second, but it won't work. This is what my HTML looks like:

<html>
<head>
<script>
function showUser() {
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else { // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","ajax/test.php?q="+str,true);
  xmlhttp.send();
}
setInterval(showUser, 1000);
</script>
</head>
<body>

<div id="txtHint"><b>this will be updated</b></div>

</body>
</html>

The SetInterval won't run it. The php file is still only echo rand();. It worked at some point but I screwed something up I believe. Thanks in advance.

  • 写回答

3条回答 默认 最新

  • duanchuiwen6694 2014-05-15 18:42
    关注

    setInterval() is likely running just fine, but showUser() is ending with an error since str is not defined. Check your JavaScript console for errors.

    Either remove the reference to str, or define it somewhere.

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

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作