javascript - How to use navigator.onLine feature to POST from a form to a PHP? -
i trying use navigator.online check whether browser online or offline. if want post form html php based on condition, possible way so. below code same.
html
<script> var online = navigator.online; if(online){ function postdata() { // 1. create xhr instance - start var xhr; if (window.xmlhttprequest) { xhr = new xmlhttprequest(); } else if (window.activexobject) { xhr = new activexobject("msxml2.xmlhttp"); } else { throw new error("ajax not supported browser"); } // 1. create xhr instance - end // 2. define when xhr feed response server - start xhr.onreadystatechange = function () { if (xhr.readystate === 4) { if (xhr.status == 200 && xhr.status < 300) { document.getelementbyid('div1').innerhtml = xhr.responsetext; } } } // 2. define when xhr feed response server - start var userid = document.getelementbyid("userid").value; var pid = document.getelementbyid("pid").value; // var image = document.getelementbyid("image").value; // 3. specify action, location , send server - start xhr.open('post', 'login.php'); //xhr.open('post', 'config.php'); xhr.setrequestheader("content-type", "application/x-www-form-urlencoded"); xhr.send("userid=" + userid + "&pid=" + pid); //xhr.send("&pid=" + pid); // 3. specify action, location , send server - end } } </script>
Comments
Post a Comment