admin管理员组文章数量:1427643
Greetings!
I need to dynamically check if a session variable changes every few seconds. For example, the PHP session variable "x" may have a value of "1", and then after five seconds, has a value of "2".
The PHP session variable "x" is changed by using a form. If the session variable changes, I need the page to reload.
How can I reload the page if the session variable changes without refreshing the page manually?
Greetings!
I need to dynamically check if a session variable changes every few seconds. For example, the PHP session variable "x" may have a value of "1", and then after five seconds, has a value of "2".
The PHP session variable "x" is changed by using a form. If the session variable changes, I need the page to reload.
How can I reload the page if the session variable changes without refreshing the page manually?
Share Improve this question asked Aug 18, 2010 at 2:45 John CoderJohn Coder 211 silver badge2 bronze badges 2- are you using pure JavaScript? Any libraries? – krx Commented Aug 18, 2010 at 2:49
- I'm not sure what to use. I thought JavaScript/AJAX would be a good idea but I need a code example. I was thinking a simple if/then statement but I need to get the current PHP session variable and use JavaScript to refresh the page, doing all of this WITHOUT reloading the page. So, the PHP session variable is checked using AJAX and pared to the current value. – John Coder Commented Aug 18, 2010 at 2:51
2 Answers
Reset to default 3AJAX is a good solution for something like this. Just make a request to script that will return the current value for the session variable. If it is different, then reload.
So, when your page first loads, you have something like
NOTE: This example is relying on jquery library.
<script type="text/javascript">
var currentSessionValue = <?php echo $_SESSION['something']; ?>;
// pseudo code
setTimeout(checkVariableValue, 5000);
function checkVariableValue() {
$.ajax({
url: 'checkMyValue.php',
success: function(newVal) {
if (newVal != currentSessionValue);
currentSessionValue = newVal;
alert('Value Has Changed.');
doSomethingDifferent_or_refreshPage();
}
});
}
</script>
checkMyValue.php
<?php
start_session();
echo $_SESSION['myVariable'];
?>
i did like this in my code
<script>
function myFunction() {
var name = document.getElementById("name").value;
var email = document.getElementById("email").value;
var password = document.getElementById("password").value;
var phone = document.getElementById("phone").value;
// Returns successful data submission message when the entered information is stored in database.
var dataString = 'name=' + name + '&email=' + email + '&password=' + password + '&phone=' + phone;
if (name == '' || email == '' || password == '') {
alert("Please fill all fields");
} else {
// AJAX code to submit form.
$.ajax({
type: "POST",
url: "signupAjax.php",
data: dataString,
cache: false,
success: function(html) {
var isRegistered = '<?php echo $_SESSION['name'];?>';
var url = '<?php $url = $site_root.'index.php'; echo $url;?>';
if(isRegistered.length > 0)
{
window.location = url;
}
else
{
alert(html);
}
}
});
}
return false;
}
</script>
本文标签: javascriptPHPAJAX check if session variable changedStack Overflow
版权声明:本文标题:javascript - PHPAJAX check if session variable changed - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745506719a2661277.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论