最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - PHPAJAX check if session variable changed - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 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.

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>
发布评论

评论列表(0)

  1. 暂无评论