up vote 2 down vote favorite

i want to send a javascript array to php using jquery ajax.

$.post("controllers/ajaxcalls/users.php",
{
    links: links
});

where the second 'links' is a javascript array.

when i've got this array:

'1' ...
    '1' => "comment1"
    '2' => "link1"
    '3' => "link2"
    '4' => "link3"
'2' ...
    '1' => "comment2"
    '2' => "link4"

then using:

var jsonLinks = JSON.stringify(links);
alert(jsonLinks);

will give me:

[null,[null,"comment1","link1","link2","link3"],[null,"comment2","link4"]]

seems to me that something is wrong. what are the null:s and i cant use json_decode on php side to get the elements.

what function should i use to convert it to json and how do i access it on the php side?

tried this http://code.google.com/p/jquery-json/ but it will give exactly the same output as JSON.stringify() (they also say that in the documentation).

have struggled with this in some hours now...would appreciate some SPECIFIC help.

i just want to send an array from javascript to php...why is that so damn difficult:/

link|flag

Why did you ask this question again? You already asked stackoverflow.com/questions/2138812 – Justin Johnson Jan 26 at 12:58

3 Answers

up vote 2 down vote accepted

Answering the null part, JavaScript arrays are numeric and zero based:

>>> var foo = [];
>>> foo[5] = 'Five';
"Five"
>>> foo
[undefined, undefined, undefined, undefined, undefined, "Five"]

On the contrary, PHP allows missing (and mixed) keys:

<?php

$foo = array();
$foo[5] = 'Five';
print_r($foo);

?>

Array
(
    [5] => Five
)

If you want to use arrays, I suggest you make them zero-based and prevent missing values. Otherwise, you could probably use objects instead.

link|flag
thank u!! with that explanation i figured out everything! – weng Jan 26 at 13:27
up vote 2 down vote

There are some jQuery plugin that can encode to JSON (and decode from JSON).


For instance, you can take a look at jquery-json (quoting) :

This plugin exposes four new functions onto the $, or jQuery object:

  • toJSON: Serializes a javascript object, number, string, or arry into JSON.
  • evalJSON: Converts from JSON to Javascript, quickly, and is trivial.
  • secureEvalJSON: Converts from JSON to Javascript, but does so while checking to see if the source is actually JSON, and not with other Javascript statements thrown in.
  • quoteString: Places quotes around a string, and inteligently escapes any quote, backslash, or control characters.
link|flag
that give me the same output as using JSON.stringify(). if its nothing wrong with the output how do i access the elements? i tried with alert(jsonLinks[1][1]) but it gave an undefined. – weng Jan 26 at 11:43
up vote 1 down vote

I'm not sure how you are trying to read this in your PHP script, but if it is related to you having 1-based arrays rather than 0-based arrays, then in javascript, you can remove the nulls (zeroth element) with:

var jsonLinks = JSON.stringify(links.slice(1)); 
link|flag
i dont get the null:s...what are those? i have never access arrays sent through javascript but i guess its $links = json_decode($_POST['links']), then just use $links[1][1] etc. but it doesnt work. so i guess something is wrong cause why do i get all the NULLS? – weng Jan 26 at 11:46
You get the nulls because you are starting your array at index "1". Javascript arrays are zero-based, so index 0 (links[0], links[1][0] and links[2][0]) will be "null" (not set) – Graza Jan 26 at 17:47

Your Answer

 
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.