0

I've a php file which processes a query and returns an array.

How do i get these and array and parse them and display them using ajax.

I call the file using ajax. Its used to display the matching products while typing a text box with the price...

responseText doesn't return anything...

4
  • Are you using XMLHttpRequest or a JavaScript library like jQuery to make the AJAX call? Also, what format is the PHP data being serialized to? Commented May 3, 2010 at 5:41
  • 1
    Considering AJAX can only receive outputted text. A PHP return call won't be recognized, because it can only be received by PHP. You have to echo or print things in order for AJAX to get them. Try using JSON. Commented May 3, 2010 at 5:42
  • I'm using XMLHttpRequest and my php file will just send the array. If it can only get outputted text, then i should be able to attach the name and price of each product so that when i click a product it can display the price of it.. So how can i do this? Commented May 3, 2010 at 5:50
  • I think i could be more clear. I've a form with text fields. when i type in a text box i should get the products along with their price from the db matching the specific string (this is what i'm trying to get through ajax) and then when i list them and the user selects the product the price of that specific product should be listed on another text box. I'm not expecting full set of code, Gimme an idea, so that i'll continue, Commented May 3, 2010 at 6:00

1 Answer 1

1

Your data needs to be encoded in a format that JavaScript can understand, like JSON. You want to serialize the PHP array and return that as the HTTP Response, more information on how to JSON serialize data can be found here. Once the data is serialized, you can parse it in the ResponseText as though it were a JavaScript object (i.e. you can pull data like this: ResponseText[0].some_key)

I should note that jQuery makes this very, VERY easy. Like... this easy:

var url = '/json-data.php?id=2';

$.getJSON(url, function(data) {
    $("#target").text(data.some_key);
}
Sign up to request clarification or add additional context in comments.

Comments

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.