fidtal
fidtal

Reputation: 57

Getting PHP session value in JQuery

I have a progress bar and a button.

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.

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.

This is the code on main page:

<div id ="resultDiv" style="text-align:center;"></div>
 <input type="hidden" id="hdnSession" data-value="@Request.RequestContext.HttpContext.Session["ID"]" />

This is the JQuery file (where I check if user has active giftcode using AJAX):

$(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);
  })
});

And this is the .php file which does the checking:

<?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'<br><hr><h1 style="color: #5cb85c;">Your generated gift code:</h1><br><pre><kbd style="background-color: #5cb85c; color: #000;">'.$randomcode.'<kbd></pre><hr><p class="small" style="font-weight:bold;">You can generate a new code in 24 hours.</p>';
    } else {
      echo 'You already have an active gift code!';
    }
  }
}
?>

So the problem is, var sessionValue= $("#hdnSession").data('value'); returns 0 every time although I'm sure the user $_SESSION['ID'] is set. If I generate a gift code, the UserID will get set to 0 every time.

Upvotes: 2

Views: 1967

Answers (2)

ScaisEdge
ScaisEdge

Reputation: 133360

If your "main page" is a php page you can use this (getting from php the $_SESSION['ID'] value and assign to value for the hidden input field

<div id ="resultDiv" style="text-align:center;"></div>
     <input type="hidden" id="hdnSession"  value="<?php echo $_SESSION['ID']; ?>" />

Upvotes: 1

fidtal
fidtal

Reputation: 57

I can just use $_SESSION['ID'] in the PHP file.. don't know why I tried to get this so complicated. Sorry.

Upvotes: 1

Related Questions