开发者

How to return multiple items with AJAX and PHP

开发者 https://www.devze.com 2023-03-16 19:06 出处:网络
Is it possible to return multiple items via AJAX and PHP? I am reading up on it and it seems just l开发者_如何学JAVAike an echo in PHP and responseText via Javascript. Can I return an array of items v

Is it possible to return multiple items via AJAX and PHP? I am reading up on it and it seems just l开发者_如何学JAVAike an echo in PHP and responseText via Javascript. Can I return an array of items via PHP and convert it into Javascript or HTML?


It depends on the data format your PHP response is sending - it sounds like the examples you're seeing are just plain text. The best format in my opinion is JSON which can send an array of multiple results, and even a multi-dimensional array many levels deep.

See json_encode() in PHP and JSON examples on jquery.com.


You can use the JSON format to return multiple values in AJAX: http://www.php.net/manual/en/function.json-encode.php

You can then decode it in javascript. This function allows you to do it in jQuery: http://api.jquery.com/jQuery.getJSON/

0

精彩评论

暂无评论...
验证码 换一张
取 消