#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    2

    Include Session variable in Ajax


    Hi.

    I'm trying to include a session variable in ajax but nothing seems to work out. Any idea's on a good practice to do this?
    thanks!

    I've tried

    Code:
    var sessionid = "<?php echo $_SESSION['id']; ?>";
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,110
    Rep Power
    119
    What you have; would work, just add the "sessionid" variable to your AJAX request url parameter.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    2
    Originally Posted by web_loone08
    What you have; would work, just add the "sessionid" variable to your AJAX request url parameter.
    I'll try that now! but when i alert the variable it just spits out

    Code:
    <?php echo $_SESSION['id']; ?>
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    2
    Tested it, the callback is just '<?php echo $_SESSION['id']; ?>' in a string.. so javascript is taking this as a string..

    This is the php file

    Code:
    <?php
    session_start();
    $_SESSION['name'] = $_GET['name'];
    ?>
    This is my code

    Code:
    	var sessionid = "<?php echo $_SESSION['name']; ?>";
    
    	var dataString = 'sessionid=' + sessionid + '&description=' + description + '&icon=' + icon;
    
    	$.ajax({
    		type: 'POST',
    		url: 'save.php',
    		data: dataString,
    		success: function(data){
    			alert(data)
    		}
    	});
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    2
    SOLUTION

    Turns out i don't have to send the session along, in my save.php ( ajax file ) i can just do session_start() and use the session..

    Stupid me forgot it's a superglobal!
  10. #6
  11. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,110
    Rep Power
    119
    If it was just spitting it out as a string; the page you have your JS/jQuery in; did not support the PHP language. And..., yes, using session_start(); and setting the session variable; is superglobal. I had wondered why you were not doing it that way; but just assumed you had a reason for sending session variable in an AJAX query string.

    Glad you got it working though
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    2
    Originally Posted by web_loone08
    If it was just spitting it out as a string; the page you have your JS/jQuery in; did not support the PHP language. And..., yes, it using $_session_start(); and setting the session variable; is superglobal. I had wondered when you were not doing it that way; but just assumed you had a reason for sending session variable in an AJAX query string.

    Glad you got it working though
    Thanks for the help! don't know what got into me.

    Comments on this post

    • web_loone08 agrees : your welcome, not a problem :)

IMN logo majestic logo threadwatch logo seochat tools logo