1

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?

2
  • are you using pure JavaScript? Any libraries? 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 compared to the current value. Commented Aug 18, 2010 at 2:51

2 Answers 2

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'];
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Could you provide some code as an example? I am a novice with AJAX.
Thanks. What would PERFORM_AJAX_REQUEST look like? And do you know if the PHP session variable can be obtained without calling another file?
Once the page has loaded, the only way will be by performing an additional request to get the current state of the $_SESSION data.
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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.