SamSmith SamSmith - 2 months ago 5
Javascript Question

PHP file never called from jQuery

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

Answer

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>
Comments