auto submit form / ajax

Dell / Dell laptop
BigShow January 25, 2009 at 13:03:14
Specs: Windows XP, pentium
Hey guys, I need to set a time interval in the body onload that will automaticall submit a form i have with a hidden variable. More or less I am trying to reload a page every couple minutes with new information on it. Info will constantly be going into the DB and i want to just keep pulling it out. What displays is relevent to the url example www.xyz.com?cat=all, this will autamotically go into the table all and get the ten most recent items. I need to make sure I pass the cat through whatever i use to get to the php script thta will use that to run a loop and echo out the newest ten line. Any help would be great.

See More: auto submit form / ajax

Report •


#1
January 25, 2009 at 15:57:44
Dunno if it will be of use, but I modified an ajax script I use at times for posting data to send the date every couple of seconds. Below are the two files - the html page and php page respectively.

<html>
<head>
<script language="javascript">

function postIt(vData) {

try {

   var url = "posted.php";
   var params = "cmd=update&cat=" + vData;

   http.open("POST", url, true);
   http.onreadystatechange = ajaxGetRes;

   http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   http.setRequestHeader("Content-length", params.length);
   http.setRequestHeader("Connection", "close");

   http.send(params);
}

catch(e) { alert(e); }
}

function ajaxGetRes() {

   if(http.readyState == 4 && http.status == 200) {
      document.getElementById("resTxt").innerHTML = http.responseText;
   } else {
      document.getElementById("resTxt").innerHTML = "ajax error";
   }
}

function getXHTTP() {

   var xhttp;

   try { 
      xhttp = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
      try {
         xhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
           try {
              xhttp = new XMLHttpRequest();
           } catch (e3) {
              xhttp = false;
           }
      }
   }

   return xhttp;
}

var http = getXHTTP();

</script>
</head>
<body onload="setInterval('postIt(Date())', 2000)">
<div id="resTxt"></div>
</body>
</html>

===================================
<?php

   if(isset($_POST["cmd"]) && $_POST["cmd"] === "update") {

      foreach($_POST as $key => $value) {
         echo "key = " . $key . " and value = " . $value . "
";
      }
   }
?>

Hope that helps


Report •
Related Solutions


Ask Question