title: Update PHP Sessions with Ajax
body: <p>Basically I want to resize my page and objects according to screen resolution. I can't use Media Queries because I need to resize some flash and JS objects as well by passing the width, not only HTML elements.</p>
  
  <p>So, I was thinking to store the screen width in a PHP SESSION variable using AJAX request. To do so, I have below code. But the issue is, the session variable gets updated after next page refresh. I am sure I am missing pretty much straight forward thing, but just can't figure it out. Any help will be appreciated.</p>
  
  <p>Main.php:</p>
  
  <pre><code>if (!isset($_SESSION)) {
      session_start();
  }
  &lt;script&gt;
  $.ajax({
      async: false,
      type:'POST',
      url: 'screen.php',
      data: 'width=1400'
      }).complete(function(data){
          alert(&lt;?php echo "SESSION value in main.php: ".$_SESSION['test_width'];?&gt;);
          alert(data.responseText);
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>screen.php</p>
  
  <pre><code>if(isset($_POST['width'])){
      if (!isset($_SESSION)) {
      session_start();
      }
      $_SESSION['test_width'] = $_POST['width'];
      echo "SESSION value in screen.php: ".$_SESSION['test_width'];
  }
  </code></pre>
  
  <p>outputs:</p>
  
  <pre><code>SESSION value in main.php: 1300 //LAST VALUE
  SESSION value in screen.php: 1400 // CURRENT VALUE
  </code></pre>