2

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.

5
  • What is @Request.RequestContext.HttpContext.Session["ID"]? It looks like the nested double quotes might cause problems. Commented Oct 22, 2015 at 19:32
  • 1
    I'm confused... @Request.RequestContext.HttpContext.Session["ID"] is ASP.NET code (or are you using some other framework?). You should probably go with <?php echo $_SESSION['ID'] ?> Commented Oct 22, 2015 at 19:33
  • Copied it from a method I found.. Didn't find any better ways. Commented Oct 22, 2015 at 19:38
  • You should go for json_encode and json_decode Commented Oct 22, 2015 at 19:39
  • at least understand what language you are copying from Commented Oct 22, 2015 at 19:42

2 Answers 2

1

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']; ?>" />
Sign up to request clarification or add additional context in comments.

6 Comments

But you'll need to echo the value from PHP. As it stands, your code sets the value to a string "$_SESSION['id']" instead of the variable's value. See this example.
Inside a double quote the php var are evaluated
But the double quotes are in the HTML. I don't see any indication that a string of HTML is being outputted from PHP.
In the answer there is at the beginning if your main page is a php page. ......mean if for this part of code are valid the php rule ....
Assuming that the page is executed as PHP, you still need to use PHP code to access a PHP variable. If you are outputting all of the HTML as a string from PHP, I suggest that you indicate that in your answer.
|
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.