0

I have the following script in a HTML file that is called when the document is loaded:

 <script>
      $(document).ready(function(){
                setInterval(function() {
                   $.get('check_session.php', function(data) {
                    alert('Load was performed.');
                });
            }, 5000);
       });
     </script>

The alert message will be called on the interval, but the PHP is not actually called because nothing is echoed.

PHP file: check_session.php

 <?php  
     //check_session.php  
     session_start();  
    echo '<script language="javascript">';
    echo 'alert("successful")';
    echo '</script>';

    echo $_SESSION['user_token'];

     if(isset($_SESSION['user_token']))  
     {  
          echo '0';     //session not expired       
     }  
     else  
     {  
          echo '1';     //session expired  
     }  
     ?>  

Essentially, I am trying to call the PHP file, check_session.php, on a five second interval. This is one of my first times implementing jQuery, and after much research, I am still lost.

Any suggestions as to why the php file is not called are appreciated.

---UPDATE:

From Network tab:

The PHP file is called

5
  • What errors do you get in the console? Does the network tab of the developer tools show the PHP file being called? Commented Sep 18, 2016 at 15:43
  • I would suggest opening the Developer Tools for your browser, choose the "Network" tab, and having a look at the response for "check_session.php". The result of your echo statements will be there. Commented Sep 18, 2016 at 15:43
  • @j08691 No errors in the console, and the network tab shows that PHP file being call. Commented Sep 18, 2016 at 15:46
  • @kkaosninja The network tab shows that PHP file being call. What do you mean "response" for the PHP file? Commented Sep 18, 2016 at 15:47
  • @SamSmith Every HTTP call consists of a request and a response, which you should be able to see in your network tab for each HTTP call. All I am asking you to do is look at the contents of the response that the server sent, for your request to check_session.php Commented Sep 18, 2016 at 15:52

2 Answers 2

3

Check data parameter

 <script>
      $(document).ready(function(){
                setInterval(function() {
                   $.get('check_session.php', function(data) {
                    alert('Load was performed.');
                    console.log('data: '+data)
                });
            }, 5000);
       });
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

The data parameter shows the echoed variables from the PHP file.
0

the problem might be here: $.get needs URL and other optional params. you are simply giving filename. I think you should add appropriate path also. try it !!

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.