I have an XML file that changes every few seconds. I would like to saw the data in a div (with the help of jQuery/ajax). I manage to make data autoreload with the help of simplexml_load_string
, but because the reload is happening only in a div, I can't use PHP commands like $_POST to keep and the old data between reloads. I would like to have a very simple example of code that takes a variable between ajax div reloads. I try this so far but it is not working.
First try
html file:
<script src="jQuery_v2.1.1.js" type="text/javascript">test</script>
<script>
$(document).ready(function(){
setInterval(function(){
$("#screen").load('phpcode.php')
}, 5000);
$.ajax({
type: 'POST',
url: 'test.php',
data: {'varcount': $varcount}
});
});
</script>
<div id="screen"></div>
phpcode.php
<div>
<?php
$varcount = $_POST['varcount'];
$varcount += 1;
echo "Test ".$varcount."<br>";
?>
</div>
Second try (I tryed to convert javascript variable to PHP so that i can POST it, and then convert it again to javascript so that i can sent it back via jquery/ajax).
test.php:
<script src="jQuery_v2.1.1.js" type="text/javascript">test</script>
<script>
$(document).ready(function(){
setInterval(function(){
$("#screen").load('phpcode.php')
}, 5000);
$.post("test.php", { varcount: varcount } );
});
</script>
<div id="screen"></div>
phpcode.php
<div>
<?php
$varcount = $_POST['varcount'];
$varcount += 1;
echo "Test ".$varcount."<br>";
?>
<script>
var varcount = <?php echo "$varcount"; ?> // convet it to javascript variable
</script>
</div>
Questions are:
1. What is wrong in the above code/s and I can't POST any variable?
2. Because XML file is very big (120kb) and reloads every few seconds might be problem for the server, is there any better way to do it?
3. I will have to POST many different array in my final code. How would be able to POST 2 different arrays?
Thanks in advance. Any help would be appreciated.