title: getting state and time of jwplayer
body: <p>I am trying to get the state and time of the Jwplayer but I get an "undefined" message when trying to see the result of both. I am using Jwplayer 5.7. Please if you can look at my code to see what I have done wrong, that would be awesome. Also my jquery.post() to the database does not fire anything (might be because i am not getting a state). In my console log under networking I cant see the file being called upon. </p>
  
  <p>One final note, I am trying to send the message to the database in a post when the user clicks out of the browser, so far I have not been able to get that to work yet. I think I might be on the right path so please any further guidance would be appreciated.</p>
  
  <p>Thanks in advance.</p>
  
  <pre><code>&lt;?php 
      $sessionid = 9;
      $channelid = 2;
      $ipaddress = get_client_ip();
  
          function get_client_ip() {
           $ipaddress = '';
           if (isset($_SERVER['HTTP_CLIENT_IP']) )
               $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
           else if( isset($_SERVER['HTTP_X_FORWARDED_FOR']) ) 
               $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
           else if( isset($_SERVER['HTTP_X_FORWARDED']) )
               $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
           else if(isset($_SERVER['HTTP_FORWARDED_FOR']) )
               $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
           else if(isset($_SERVER['HTTP_FORWARDED']) )
               $ipaddress = $_SERVER['HTTP_FORWARDED'];
           else if(isset($_SERVER['REMOTE_ADDR']) )
               $ipaddress = $_SERVER['REMOTE_ADDR'];
           else
               $ipaddress = 'UNKNOWN';
  
           return $ipaddress; 
      }
      ?&gt;
      &lt;!Doctype&gt;
      &lt;HTML&gt;
      &lt;head&gt;
          &lt;!--&lt;link rel="stylesheet" href="css/grid.css" type="text/css"&gt;--&gt;
          &lt;link rel="stylesheet" href="css/main.css" type="text/css"&gt;
          &lt;script type="text/javascript"&gt;&lt;/script&gt;
          &lt;!-- Cll on slider.js here --&gt;
      &lt;/head&gt;
          &lt;body&gt;
              &lt;!--&lt;div class="side_banner"&gt;&lt;img src="images/logo1.png"/&gt;&lt;/div&gt;--&gt;
                  &lt;div class="container"&gt;
                      &lt;div class="grid4 omega"&gt;
                          &lt;div class="banner"&gt;
                              &lt;img src="images/banner.png"/&gt;
                          &lt;/div&gt;
                          &lt;div id="video_container"&gt;&lt;/div&gt;
                          &lt;div id="timer"&gt;&lt;/div&gt;
                          &lt;div id="state"&gt;&lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;script type="text/javascript" src="js/jwplayer.js"&gt;&lt;/script&gt;   
                  &lt;script type="text/javascript"&gt;
                      jwplayer("video_container").setup({
                           'modes': [
                          {type: 'flash', src: 'includes/jwplayer/player.swf'},
                          {type: 'html5'}
                      ],
                          'height': 405,
                          'width': 720,
                          'autostretch':'false',
                          'wmode':'transparent',
                          'controls':false,
                          'autostart':false,
                          'file':'&lt;?php echo json_encode($sessionid); ?&gt;.flv',
                          'repeat':true,
                      });
  
                      var state = jwplayer("video_container").getState();
                      var time = jwplayer("video_container").getPosition();
                      document.getElementById('timer').innerHTML = 'The time is ' + time;
                      document.getElementById('state').innerHTML = 'The player is in a ' + state +' state';
                      if(state == "PAUSED"){
                          document.getElementById('timer').innerHTML = 'On pause, the time is ' + time;
                      }if(state == "IDLE"){
                          //try to push to database
                          try{
                              $.post(
                                  'save_time.php',
                                  {time:time,
                                  ipaddress:&lt;?php echo json_encode($ipaddress);?&gt;,
                                  channelid:&lt;?php echo json_encode($channelid);?&gt;,
                                  sessionid:&lt;?php echo json_encode($sessionid); ?&gt;
                                  },
                                  function data(){
                                      alert(data);
                                      if(data == 'failed'){
                                          alert(failed);
                                      }
                                  }
                              );
                          }catch(err){}
                      }
  
                      //take care of the exit
                      window.onbeforeunload = function(){
                          console.log('leeeeeeaaavvvving');
                          //alert('Are you sure you want to close out?');
                          var formData = {time:time, ipaddress:&lt;?php echo json_encode($ipaddress); ?&gt;, sessionid:&lt;?php echo json_encode($sessionid); ?&gt;, channelid: &lt;?php echo json_encode($channelid); ?&gt;};
                          $.ajax({
                              url:'save_time.php',
                              type:'POST',
                              data: formData,
                              async: false,
                              timeout: 2000,
                               success: function(data, textStatus, jqXHR)
                              {
                                  //data - response from server
                                  console.log(data);
                              },
                              error: function (jqXHR, textStatus, errorThrown)
                              {
                                  console.log('errrororrrrrrr');
                              }
                          });     
                      }
  
                  &lt;/script&gt;
                  &lt;!--&lt;div class="bottom_buttons"&gt;
                      &lt;div class="button left"&gt;Start Over&lt;/div&gt;
                      &lt;div class="button right"&gt;Continue&lt;/div&gt;
                  &lt;/div&gt;--&gt;
          &lt;/body&gt;
      &lt;/html&gt;
  </code></pre>