php - Refresh javascript calculations without reloading page -
i have submit button uses javascript xmlhttp request call php page that's sole function write kml file google earth window on main page. php page not viewable within web browser html.
the formulas in php file work intended. problem i'm having after manually press submit first time script continue repeat every 5 seconds until manually reload page (or press button stop script). because plan on having multiple users @ same time viewing page each user assigned random 5 digit number hold session information , create kml files within newly created session folder until reload page (which create new session number user).
because each user designated unique session id page cannot reload php calculations repeat. because of have return false line @ end of javascript function.
i able use javascript call setinterval repeat function without reloading page. if page reload created kml file not viewable within new session. let me know if has suggestions. below applicable code.
div id on main index.php page
<div> <form id="kmlsubmit" name=kmlsubmit > <input class="kmlsubmit" type="submit" value="create kml" onclick="createkml()"/> </form> </div>
javascript function on main index.php page
function createkml() { $('#kmlsubmit').submit(function () { $.get("generatekml.php",function(data,status){ }); //alert("generating kml files!"); if (window.xmlhttprequest) {// code ie7+, firefox, chrome, opera, safari xmlhttp=new xmlhttprequest(); } else {// code ie6, ie5 xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { document.getelementbyid("kmldetails").innerhtml=xmlhttp.responsetext; } } xmlhttp.open("post","generatekml.php?session=" + session,true); xmlhttp.send(); return false; }); }
let me know if has suggestions on how this. help.
make createkml()
external, call on kmlsubmit
submit event, set timeout function of 5 sec , clear timeout on next submition restart process this:
var mytimer = false; function createkml() { var xmlhttp = (window.xmlhttprequest) ? new xmlhttprequest() : new activexobject("microsoft.xmlhttp"); xmlhttp.onreadystatechange = function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { document.getelementbyid("kmldetails").innerhtml=xmlhttp.responsetext; } } xmlhttp.open("post", "generatekml.php", true); xmlhttp.send("session=" + session); mytimer = settimeout(function(){createkml()}, 5000); } document.getelementbyid('kmlsubmit').onsubmit = function(event){ (event.preventdefault) ? event.preventdefault() : event.returnvalue = false; //prevent form submitting , reloading page if(mytimer) cleartimeout(mytimer); createkml(); }
Comments
Post a Comment