fohanlon Posted July 18, 2010 Share Posted July 18, 2010 Hi Guys I want to be able to autosave a form content every X seconds, I coded it using php and javascript getHTTPObject() function but it will not work. Can anyone point me to a php solution to autosave a form Kind Regards, Fergal. Quote Link to comment Share on other sites More sharing options...
jcbones Posted July 18, 2010 Share Posted July 18, 2010 I think you need to use setInterval(). <script type="text/javscript"> var seconds = 5; var interval = seconds * 1000; setInterval('ajaxFunction', interval); //AJAX Function </script> Quote Link to comment Share on other sites More sharing options...
fohanlon Posted July 18, 2010 Author Share Posted July 18, 2010 Hi Below is the code I am using (found after a net search - not mine). I load init() in body on page load.: function init() { window.setInterval(autoSave, 20000); // 20 seconds } function autoSave() { var status = document.getElementById("status").value; var params = "?status=" + status; var http = getHTTPObject(); http.onreadystatechange = function() { if(http.readyState == 4 && http.status == 200) // 200 = HTTP OK { // alert(http.responseText); alert("All data for this current participant has been auto saved"); } }; http.open("POST", "mypageaddress.php" + params, true); http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); http.setRequestHeader("Content-length", params.length); http.setRequestHeader("Connection", "close"); http.send(params); } //cross-browser xmlHTTP getter function getHTTPObject() { var xmlhttp; /*@cc_on @if (@_jscript_version >= 5) try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp = false; } } @else xmlhttp = false; @end @*/ if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { try { xmlhttp = new XMLHttpRequest(); } catch (e) { xmlhttp = false; } } return xmlhttp; } The page will refresh and I have a php function to update mysql tab if($_SERVER["REQUEST_METHOD"] == "POST") { } but this is not being executed. Thanks, Fergal. Quote Link to comment Share on other sites More sharing options...
jcbones Posted July 19, 2010 Share Posted July 19, 2010 Try: //Change: window.setInterval(autoSave, 20000); // 20 seconds //To: window.setInterval("autoSave()", 20000); // 20 seconds //Change: var params = "?status=" + status; //To var params = "status="+status; After all of that, open the page in firefox, and select 'Tools->Error Console'. Watch the error console for any errors the script might throw. Quote Link to comment Share on other sites More sharing options...
fohanlon Posted July 19, 2010 Author Share Posted July 19, 2010 Hi Jcbones I did as you mentioned and I put a echo "<script>alert('here');</script>"; message in my php code on form submission but in FF nothing is happening. Not even the alert I have in the function AutoSave is appearing. really stuck on this one. Thanks for the help. Fergal. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.