首页 文章

从AJAX发布表单自动更新数据

提问于
浏览
0

我正在使用AJAX脚本从表单发布数据并从php文件返回已处理的数据 . 脚本如下 .

function loadXmlDoc(hashtag){
    var xmlhttp;
    if (window.XMLHttpRequest){
        xmlhttp = new XMLHttpRequest();
    }
    else{
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
            document.getElementById("ajaxify").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("POST", "demo_ajax3.php", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send("hashtag=" + hashtag);
    }

但是我想自动更新结果 . 我如何让它自动更新,以便检查更多数据?或者最好的方法是什么?

似乎没有什么可以让AJAX表单从我看到的内容中自动刷新,所以任何帮助都会非常适合

1 回答

  • 0

    试试这种方式 . 它应该每10秒自动更新一次结果 .

    var xmlhttp;
    if (window.XMLHttpRequest){
        xmlhttp = new XMLHttpRequest();
    }
    else{
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function(){
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
        document.getElementById("ajaxify").innerHTML = xmlhttp.responseText;
      }
    }
    function loadXmlDoc(hashtag){
      var repeat = function () {
        xmlhttp.open("POST", "demo_ajax3.php", true);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.send("hashtag=" + hashtag);
      };
      repeat.hashtag = hashtag;
      setInterval(function() { repeat(); },10000);
    }
    

相关问题