Matt Jameson
Matt Jameson

Reputation: 217

obtaining PHP variable in jQuery AJAX

In my php file 'login_success.php', I have a the variable $_COOKIE["user"]

Would it be possible to return that variable within a jQuery Ajax statment such as this one. ive made a guess with Var UserName =:

function StartAjax(NameID){
    $.ajax({
      type: "POST",
      url: "login_success.php",
      cache: false,
      data: "name=Peter&location=Sheffield",
      success: function(html, status){
        $("#"+NameID).append(html);
        //$('#status').append(status);
        var userName = 

      } 
    });

Upvotes: 0

Views: 38

Answers (1)

Raekye
Raekye

Reputation: 5131

login_success.php:
echo $_COOKIE['user'];

Ajax asks for the page, returns the output/content (what is echoed). Please note: Javascript is client side. PHP is server side. So you can't directly access PHP variables, but as done above, PHP can "give" the client the info it needs. If you're sending a lot of data, you can json_encode it for the ajax request. (you can learn about JSON on your own)

Also note, Javascript can access cookies, but I'm assuming this is just a sample question.

Upvotes: 1

Related Questions