Casey Dwayne
Casey Dwayne

Reputation: 2169

Returning data from php called via jQuery (ajax)

I'm having trouble returning data from a php page.

test = function(data){
    alert(data);
}

newContent = function(var1){
  $.ajax({
      url: 'path/to/item.php&var1='+var1,
      success: function(data){
        test(data);
      }
  });
}

Which should return "Success" via echo 'Success!'; on the item.php page.

Why is it returning undefined?

Upvotes: 1

Views: 56

Answers (2)

Petr R.
Petr R.

Reputation: 1237

Your URL is incorrect:

url: 'path/to/item.php&var1='+var1,

Use this instead:

url: 'path/to/item.php?var1='+var1,

JsFiddle here

Upvotes: 1

jh314
jh314

Reputation: 27802

In your item.php file, you need to echo the result. Something like this:

echo $data;

rather than:

return $data;

Upvotes: 0

Related Questions