javascript - I want to update my webpage when some one else updates the database through another instance of the same page -
i want update page when update database instance of same page.but unable so. index.php home page. if block of doo() function, if(x.readystate==4 && x.status==200) never exeutes, may because status never becomes equal 200. in console getting "1000 failed load resource: net::err_insufficient_resources" . not understand why update function not waiting 10 seconds , overflowing stack. obliged solution , suggestions.
index.php
<html> <head> <title>das shuler </title> <script> function foo() { //if (str.length==0) { //document.getelementbyid("txthint").innerhtml=""; //return; //else { data=document.getelementbyid("story").value; alert(data); var xmlhttp=new xmlhttprequest(); xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { alert("state change"); data=xmlhttp.responsetext; parent=document.getelementbyid("content"); child=document.createelement("div"); news=document.createtextnode(data); child.appendchild(news); parent.appendchild(child); id=settimeout(update(data),20); alert("after settimeout"); //document.getelementbyid("txthint").innerhtml=xmlhttp.responsetext; } } alert("data sent"); xmlhttp.open("get","populate.php?story="+data,true); xmlhttp.send(); } function update(str) { data=str; //alert(data); //alert("inside update"); var x=new xmlhttprequest(); x.onreadystatechange=doo(); function doo(){ if(x.readystate==4 && x.status==200) { alert("here"); data=x.responsetext; if(data!=str) { parent=document.getelementbyid("content"); child=document.createelement("div"); news=document.createtextnode(data); child.appendchild(news); parent.appendchild(child); } } x.open("get","retrieve.php",true); x.send() id=setinterval(update(data),10000); } } </script> </head> <body onload="update()"> <div id="header"><h1 style="color:grey;align:center;">das shuler</h1><hr></div> <div id="content"> </div> <div id="make-content"><form method="post" action="populate.php"><input type="text" name="story" id="story"><input type="button" value="post" onclick="foo()"></form></div> </body>
populate.php
<?php $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "mydb"; // create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // check connection if (!$conn) { die("connection failed: " . mysqli_connect_error()); } $data=$_request["story"]; echo $data; $sql = "insert news (data) values ('$data')"; if (mysqli_query($conn, $sql)) { $last_id = mysqli_insert_id($conn); // echo "new record created successfully. last inserted id is: " . $last_id; } else { echo "error: " . $sql . "<br>" . mysqli_error($conn); } mysqli_close($conn);
?>
retrieve.php
<?php $conn=mysqli_connect("localhost","root","root","mydb"); echo "hrloo"; $sql="select data news "; $q=mysqli_query($conn,$sql); $data=mysqli_fetch_assoc($q); echo $data; mysqli_close($conn);
?>
your code seems broken. settimeout function logic not working properly. alert() right after settimeout fire on page load not included in function on setting timeout.
also settimeout() takes time in milliseconds. try putting 10,000 if need 10 sec break on window.
refer this:
settimeout(function(){ alert("after settimeout"); },10000);
Comments
Post a Comment