Reputation:
I am refreshing the content of a div with jquery every 10 seconds. I read the content from a php file named status.php
js and div:
<script>
function autoRefresh_div()
{
$("#ReloadThis").load("status.php");// a function which will load data from other file after x seconds
}
setInterval('autoRefresh_div()', 10000); // refresh div after 10 secs
</script>
<div id="ReloadThis"></div>
When i put this into my status.php it works fine:
$mystatus = 'busy';
echo $mystatus;
In the div appears after a couple of seconds: "busy".
When i try to dynamically give the $mystatus a value, it does not wrk anymore:
form:
<form method="post" action="status.php">
<input type="text" name="status"/>
<input type="submit" name="submit" value="Submit" />
</form>
and status.php:
$mystatus = $_POST['status'];
echo $mystatus;
When i open status.php it echoes the value of the input. But the javascript does not show the echo anymore in the div
Why does this not work when i create the value of $mystatus dynamically?
Upvotes: 1
Views: 420
Reputation: 1075
Every request you make to PHP is a stand alone request. So suppose you have submitted status value using POST, your php script will just remember it for that request only.
So the next time when your jquery tries to request for that value, it will not be there.
You need to store the POST value to session, and then resend the session value each time.
Something like this:
session_start();
if(isset($_POST['status']))
$_SESSION['status'] = $_POST['status'];
if(!empty($_SESSION['status'])) {
echo $_SESSION['status'];
}
else
echo "default";
Of course you have to start session before doing this using session_start();
Upvotes: 1
Reputation: 3666
The other answers are correct, but they're assuming that you've posted the status once and then want the refresh to return the same value as was previously posted.
However, if you want the refresh to also re-post the current form data, then you just have to include that post data in the load()
function, like so:
$("#ReloadThis").load("status.php", $("form").serialize());
Upvotes: 0
Reputation: 151
You have to store the $_POST['status'] in a session variable as given above or insert into the database.
Upvotes: 0