简体   繁体   中英

Passing PHP session variable to AJAX URL

I am trying to pass a session variable into an AJAX loaded page. Here is the code I am using:

 jQuery(document).ready(function(){
$("#userdetail").click(function() {
  $.ajax({
   url: "userdetail.php?id=<?php $_SESSION['uid']?>",
   success: function(msg){
     $("#results").html(msg);
   }
 });
});
});

This is the HTML URL I had working, not sure how to get this into the AJAX call:

userdetail.php?id=<?php $_SESSION['uid']?>

I should also mention that if I manually pass in the userID it works fine

url: "userdetail.php?id=1",

If this is what's actually rendering on the page:

userdetail.php?id=<?php $_SESSION['uid']?>

Then it sounds like your PHP interpreter isn't working on the server. That should output the actual value (it may need an echo or something though, I'm not sure), not the PHP code (which itself should never be output to the client).

你可以试试

  url: "userdetail.php?id=<?php echo $_SESSION['uid']?>",

Is your file a .js file? PHP interpreter doesn't read .js files by default.

I solved this by echoing the SESSION variable on the page itself rather than using GET. Thanks everyone!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM