title: Getting PHP session value in JQuery
body: <p>I have a progress bar and a button.</p>
  
  <p>When it reaches 100%, I use JQuery AJAX to check if the user already has an active giftcode in database or he doesen't. If he doesen't, I generate a new giftcode and insert it into the database.</p>
  
  <p>The generating and inserting is working just fine. My problem is, I need the user account ID in the JQuery script. I'm currently using the hidden input method and it returns my account ID 0 every time, no matter which account I use.</p>
  
  <p>This is the code on main page:</p>
  
  <pre><code>&lt;div id ="resultDiv" style="text-align:center;"&gt;&lt;/div&gt;
   &lt;input type="hidden" id="hdnSession" data-value="@Request.RequestContext.HttpContext.Session["ID"]" /&gt;
  </code></pre>
  
  <p>This is the JQuery file (where I check if user has active giftcode using AJAX):</p>
  
  <pre><code>$(function() {
  var timer = 0;
  $('#code').click(function () {
      clearInterval(timer)
      var value = 0;
      timer = setInterval(function () {
          value++;
          $('.progress-bar').width(value + '%').text(value + '%');
          if (value == 100) {
              clearInterval(timer);
  
              var sessionValue= $("#hdnSession").data('value');
  
              $.post("checkcode.php", 
              {
                  ID: sessionValue
              },
              function(data)
              {
                  $("#resultDiv").hide().html(data).fadeIn(1000);
              });
          }
      }, 10);
    })
  });
  </code></pre>
  
  <p>And this is the .php file which does the checking:</p>
  
  <pre><code>&lt;?php
  
  include_once ('connect.php');
  
  if(isset($_POST['ID']))
  {
  if(!empty($_POST['ID']))
  {
      $id = $_POST['ID'];
  
      $select_user = "SELECT * from giftcodes WHERE UserID='$id'";
  
      $query = mysqli_query($con, $select_user);
      $row = mysqli_num_rows($query);
  
      if(!$row) {
  
        $randomcode = substr(md5(uniqid(mt_rand(), true)), 0, 8);
  
        $insert_code = "INSERT INTO giftcodes (UserID, Giftcode) VALUES ('$id', '$randomcode')";
        $query = mysqli_query($con, $insert_code);
  
        echo'&lt;br&gt;&lt;hr&gt;&lt;h1 style="color: #5cb85c;"&gt;Your generated gift code:&lt;/h1&gt;&lt;br&gt;&lt;pre&gt;&lt;kbd style="background-color: #5cb85c; color: #000;"&gt;'.$randomcode.'&lt;kbd&gt;&lt;/pre&gt;&lt;hr&gt;&lt;p class="small" style="font-weight:bold;"&gt;You can generate a new code in 24 hours.&lt;/p&gt;';
      } else {
        echo 'You already have an active gift code!';
      }
    }
  }
  ?&gt;
  </code></pre>
  
  <p>So the problem is, <code>var sessionValue= $("#hdnSession").data('value');</code> returns 0 every time although I'm sure the user <code>$_SESSION['ID']</code> is set. If I generate a gift code, the UserID will get set to 0 every time.</p>