Reputation: 101
I have two php pages
page1.php
<?php
session_start();
?
<script>
function request(){
$.ajax({
cache: false,
type: "POST",
url: 'page2.php',
success: function(data) {
alert(<?php echo $_SESSION['value'];?>);
},
complete: function() {
setTimeout(function(){request();}, 2000);
}
});
};
</script>
page2.php
<?php
session_start();
$_SESSION['value'] = //Assign a random value.//
echo $_SESSION['value'];
?>
The problem is everytime Ajax is called page2.php echos the current "$_SESSION['value']" value. But in page1.php alert shows the old "$_SESSION['value']" value unless i refresh the page, after which it shows current value untill Ajax is been called again and the value gets updated. I have no clue as to why this is happening.
Upvotes: 0
Views: 35
Reputation: 3569
Just think for a second: when is your <? ?>
executed? When your html code with your script is rendered on server side. That means, it is a constant unless you refresh your page - when you get another constant. You should render the value into the response of page2 on the server side - and echo the data
you got.
Upvotes: 1