1

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?

2 Answers 2

1

Your URL is incorrect:

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

Use this instead:

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

JsFiddle here

Sign up to request clarification or add additional context in comments.

1 Comment

Nevermind, I just learned about echos in jsFiddle.
0

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

echo $data;

rather than:

return $data;

1 Comment

It is echo, as stated in OP. True return will not work for the ajax.

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.