Update : success progress but still a problem : new data reloaded after refreshing the page twice it’s ok to refresh the page once every 10 or 15 minutes but one ferfesh doesn’t display the new data from the text file though it already contains the new data came from getData.php file
getData.php code
$connect = mysql_connect("localhost","root","");
$db = mysql_select_db("mydb",$connect);
$getTextQ = "select * from text";
$getTextR = mysql_query($getTextQ);
$path = "text/";
$myfile = $path."data.txt";
$open = fopen($myfile,"w");
while($row = mysql_fetch_array($getTextR)){
$write = fwrite($open,"<div class='slide'><h1>".$row['id']."</h1><p>".$row['desc']."</p></div>");
}
$close = fclose($open);
mysql_close($connect);
index.php code
<script src="js/jquery-1.7.1.js"></script>
<script src="js/slides.min.jquery.js"></script>
<script>
$(function(){
$.ajax({
url: 'getData.php',
type: 'POST'
});
});
var txtFile = new XMLHttpRequest();
txtFile.open("GET", "text/data.txt", true);
txtFile.onreadystatechange = function() {
if (txtFile.readyState === 4) {
if (txtFile.status === 200) {
allText = txtFile.responseText;
}
}
}
txtFile.send(null);
});
</script>
html code
<div id="mydiv" class="slides_container">
<?php $path ="text/";
$open = fopen($path."data.txt","r");
$read = fread($open,50000);
$close = fclose($open);
echo $read;
?>
</div>
I would not have the php file write to a text file and then read the text file; you are doing two ajax calls / requests to the server where only one is needed.
Apart from that it could lead to errors on the server when two instances try to write at the same file at the same time.
If you change your
getData.phpto echo out the values instead of storing them in a text file, you can simplify your javascript to: