By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,385 Members | 1,767 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,385 IT Pros & Developers. It's quick & easy.

php to javascript array

P: n/a
Dear all,

I have a JavaScript function that talks to php using ajax. It can return
values from php.

Up until now, I have used it to return simple values such as integers or
text strings.

However, I wish now to return an array. Is it possible to create an
array in php, format it in some way and pass it back to javascript for use?

TIA

Steve
Sep 13 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
VK

turnitup wrote:
Is it possible to create an
array in php, format it in some way and pass it back to javascript for use?
IMHO that would be much easier achieved by using JSON (see
http://www.json.org, it also includes ready to use client-side script
module and server-side bearers in PHP).

Sep 13 '06 #2

P: n/a
turnitup napisaƂ(a):
using ajax.
to return simple values such as integers or
text strings.

However, I wish now to return an array. Is it possible to create an
array in php, format it in some way and pass it back to javascript for use?
First:
If You say - AJAX - you mean Asynchronous JavaScript and *XML* - so You
should use XML. You don't use AJAX :)
Second:
Find in google "JSON" - it help You in this problem.

--
Pozdrawiam,
Sawi
www.fruitysite.net
Sep 13 '06 #3

P: n/a


turnitup wrote:

I have a JavaScript function that talks to php using ajax. It can return
values from php.
However, I wish now to return an array. Is it possible to create an
array in php, format it in some way and pass it back to javascript for use?
There are various data interchange formats possible, one is JSON, which
in terms of JavaScript is just a subset for what JavaScript allows
syntactically for object and array literals, and which has lots of
libraries for other languages, one being PHP, see the "JSON in PHP"
entry on <http://www.json.org/>.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 13 '06 #4

P: n/a
"turnitup" <same@samewrote in message
news:45***********************@news.zen.co.uk...
Dear all,

I have a JavaScript function that talks to php using ajax. It can return
values from php.

Up until now, I have used it to return simple values such as integers or
text strings.

However, I wish now to return an array. Is it possible to create an array
in php, format it in some way and pass it back to javascript for use?
The simplest way is to return a javascript literal array like:

[1,2,3,4,5,6,7,8,9,10]

You can use the "eval" function in javascript to convert that to a native
array. Note that "eval" can be dangerous if you're not sure of your input -
it will evaluate potentially dangerous, misleading script just as easily as
the intended script.

This is the method (with a better, more secure deserializer to eliminate
eval's dangers) used by JSON. JSON is great for passing simple to
moderately complex information: it uses JavaScript literal notation and
libraries are available for many programming languages (including PHP).

JSON is probably your best choice - it will do exactly what you need now and
has room for exapansion later. It's problems generally lie in data typing
(for example there's no way to distingush primitive types like string,
number or - especially - date) and validation.

There are other options (most are XML) which allow for more complex data to
be sent. SOAP and XML-RPC are both respected solutions which provide for
complex data transfer (although they were designed primarily with remote
procedure calls in mind).

WDDX is an older, data-only, XML implementation that works well,
unfortunately it's seen very little active development recently. My own
solution, YODEL, is a data-only XML transport designed to allow complex data
transfer with typing and validation while still producing (for XML) smaller
packets than most (perhaps all) solutions.

I've got my JavaScript YODEL parser and a newer WDDX parser here (look under
"Data Exchange"):

http://www.depressedpress.com/Conten...ions/Index.cfm

Sorry - didn't mean to overload you, but there are a lot of options. ;^)

Jim Davis
Sep 13 '06 #5

P: n/a
turnitup wrote:
Dear all,

I have a JavaScript function that talks to php using ajax. It can return
values from php.

Up until now, I have used it to return simple values such as integers or
text strings.

However, I wish now to return an array. Is it possible to create an
array in php, format it in some way and pass it back to javascript for use?

TIA

Steve
Thank you everyone for your replies. Looks like JSON for now.
Sep 15 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.