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".
我需要动态检查会话变量是否每隔几秒就改变一次。例如,PHP会话变量“x”的值可能是“1”,五秒钟后,它的值是“2”。
The PHP session variable "x" is changed by using a form. If the session variable changes, I need the page to reload.
PHP会话变量“x”通过使用表单进行更改。如果会话变量改变,我需要页面重新加载。
How can I reload the page if the session variable changes without refreshing the page manually?
如果会话变量发生更改而不手动刷新页面,如何重新加载页面?
2 个解决方案
#1
3
AJAX 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.
AJAX是一种很好的解决方案。只需向脚本请求将返回会话变量的当前值。如果是不同的,则重新加载。
So, when your page first loads, you have something like
所以,当你的页面第一次加载时,你有一些类似的东西。
NOTE: This example is relying on jquery library.
注意:这个示例依赖于jquery库。
<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
checkMyValue.php
<?php
start_session();
echo $_SESSION['myVariable'];
?>
#2
0
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>
#1
3
AJAX 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.
AJAX是一种很好的解决方案。只需向脚本请求将返回会话变量的当前值。如果是不同的,则重新加载。
So, when your page first loads, you have something like
所以,当你的页面第一次加载时,你有一些类似的东西。
NOTE: This example is relying on jquery library.
注意:这个示例依赖于jquery库。
<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
checkMyValue.php
<?php
start_session();
echo $_SESSION['myVariable'];
?>
#2
0
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>