简体   繁体   中英

auto update data from AJAX posting form

I am using an AJAX script to post data from a form and return the processed data from a php file. The script is below.

    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);
    }

However I want to auto update the results. how would I get this to auto update so it checks for more data? Or what would be the best way?

There seems to be nothing about getting AJAX forms to auto refresh from what I've seen so any help would be greatly apprecaited

Try this way. It should auto-update the result every 10 seconds.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM