<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Zulius &#187; jQuery</title>
	<atom:link href="http://www.zulius.com/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zulius.com</link>
	<description>Advanced Application Development</description>
	<lastBuildDate>Mon, 30 Aug 2010 20:44:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Send multidimensional arrays to PHP with jQuery and AJAX</title>
		<link>http://www.zulius.com/how-to/send-multidimensional-arrays-php-with-jquery-ajax/</link>
		<comments>http://www.zulius.com/how-to/send-multidimensional-arrays-php-with-jquery-ajax/#comments</comments>
		<pubDate>Sat, 26 Dec 2009 14:48:46 +0000</pubDate>
		<dc:creator>Tim White</dc:creator>
				<category><![CDATA[how-to]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.zulius.com/?p=656</guid>
		<description><![CDATA[AJAX communication between the client browser and web server is a handy method of transferring data without forcing the user to refresh the page.  Typically, the data sent to the server is a simple set of form fields or flags.  However, it is also possible to submit more complex structured data.  This [...]]]></description>
			<content:encoded><![CDATA[<div class="wp-caption alignleft" style="width: 170px"><img alt="AJAX" src="/img/blog/assocArrayPhpJqueryAjax.png" title="AJAX" width="150" height="110" /><p class="wp-caption-text">javascript array</p></div>
<p><a href="http://en.wikipedia.org/wiki/Ajax_%28programming%29">AJAX</a> communication between the client browser and web server is a handy method of transferring data without forcing the user to refresh the page.  Typically, the data sent to the server is a simple set of form fields or flags.  However, it is also possible to submit more complex structured data.  This tutorial will demonstrate 3 different methods used to pass multidimensional javascript arrays using <a href="http://jquery.com/">jQuery</a> and AJAX to a <a href="http://www.php.net/">PHP</a> web server.</p>
<h2>The Wrong Way</h2>
<p>My first instinct was that multidimensional JS arrays could be passed directly to jQuery's AJAX functions.  I expected that the array would be automatically preserved in the request, and show up in PHP's $_GET/$_POST variables.  The following example shows this doesn't work.</p>
<p>The code below attempts to post a JS associative array ("data", line 9) to the web server using jQuery's <a href="http://docs.jquery.com/Ajax/jQuery.ajax">$.ajax()</a> function.  The $.ajax() function is jQuery's lowest level AJAX function, but still provides a high level approach and lots of options.</p>
<pre class="brush: xml; first-line: 1; gutter: true;">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; dir=&quot;ltr&quot; lang=&quot;en-US&quot;&gt;
    &lt;head profile=&quot;http://gmpg.org/xfn/11&quot;&gt;
        &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot; /&gt;
        &lt;title&gt;jQuery AJAX arrays&lt;/title&gt;
        &lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js&quot;&gt;&lt;/script&gt;
        &lt;script type=&quot;text/javascript&quot;&gt;
                sendAjax = function(){
                    var data = {
                                  foo:  123,
                                  bar:  456,
                                  rows: [
                                          {
                                            column1 : 'hello',
                                            column2 : 'hola',
                                            column3 : 'bonjour',
                                          },
                                          {
                                            column1 : 'goodbye',
                                            column2 : 'hasta luego',
                                            column3 : 'au revoir',
                                          },
                                        ],
                                  test1:{
                                          test2: {
                                                   test3:  'baz'
                                                 }
                                        }
                                };

                    $.ajax({
                            type:           'post',
                            cache:          false,
                            url:            './ajax/',
                            data:           data
                           });
                }

        &lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;input type=&quot;button&quot; style=&quot;width: 130px; height: 60px&quot; value=&quot;send AJAX&quot; onclick=&quot;sendAjax();&quot; /&gt;
    &lt;/body&gt;
&lt;/html&gt;
</pre>
<p>When the button is clicked, the following data shows up in PHP's $_POST variable:</p>
<pre class="brush: plain;">
Array
(
    [foo] =&gt; 123
    [bar] =&gt; 456
    [rows] =&gt; [object Object]
    [test1] =&gt; [object Object]
)
</pre>
<p>As shown, the first level of the array data is preserved (keys "foo" and "bar").  However, jQuery simply calls .toString() on the nested arrays at keys "rows" and "test1", and they get passed to PHP with the useless values "[object Object]".</p>
<h2>Method 1 - Form input arrays</h2>
<p>It's possible to name the keys of the JS array as HTML form input arrays (ie. named with square brackets) so that <a href="http://php.net/manual/en/language.variables.external.php">PHP decodes the array's key names as nested arrays</a>.  I don't recommend this method because it does not maintain the JS array's original structure in the Javascript.  However, it can be used if you just need to get the AJAX request working without using JSON.</p>
<p>
The original JS "data" array can be renamed/restructured as:
</p>
<pre class="brush: jscript;">
sendAjax = function(){
	var data = {
				  foo:  123,
				  bar:  456,
				  'rows[0][column1]':    'hello',
				  'rows[0][column2]':    'hola',
				  'rows[0][column3]':    'bonjour',
				  'rows[1][column1]':    'goodbye',
				  'rows[1][column2]':    'hasta luego',
				  'rows[1][column3]':    'au revoir',
				  'test1[test2][test3]': 'baz'
				};
	$.ajax({
			type:           'post',
			cache:          false,
			url:            './ajax/',
			data:           data
		   });
}
</pre>
<p>And the data is correctly received in PHP's $_POST variable:</p>
<pre class="brush: plain;">
Array
(
    [foo] =&gt; 123
    [bar] =&gt; 456
    [rows] =&gt; Array
        (
            [0] =&gt; Array
                (
                    [column1] =&gt; hello
                    [column2] =&gt; hola
                    [column3] =&gt; bonjour
                )

            [1] =&gt; Array
                (
                    [column1] =&gt; goodbye
                    [column2] =&gt; hasta luego
                    [column3] =&gt; au revoir
                )
        )

    [test1] =&gt; Array
        (
            [test2] =&gt; Array
                (
                    [test3] =&gt; baz
                )
        )
)
</pre>
<h2>Method 2 - JSON string parameter</h2>
<p>This method converts the JS array to a <a href="http://www.json.org/">JSON</a> string, and passes it as a POST or GET parameter, which can be manually decoded back into an array by PHP.  Modern browsers (Firefox 3, IE 8) now have a native JSON object that can be used to stringify JS arrays to JSON strings.  The following example uses the <a href="http://developer.yahoo.com/yui/json/">YUI JSON utility</a> to convert the JS array to a string ( <a href="http://www.json.org/json2.js">json2</a> is another populary lightweight utility for handling JSON).</p>
<pre class="brush: jscript;">
sendAjax = function(){
	var data = {
				  foo:  123,
				  bar:  456,
				  rows: [
						  {
							column1 : 'hello',
							column2 : 'hola',
							column3 : 'bonjour',
						  },
						  {
							column1 : 'goodbye',
							column2 : 'hasta luego',
							column3 : 'au revoir',
						  },
						],
				  test1:{
						  test2: {
								   test3:  'baz'
								 }
						}
				};
	data = YAHOO.lang.JSON.stringify(data);

	$.ajax({
			type:           'post',
			cache:          false,
			url:            './ajax/',
			data:           {myJson:  data}
		   });
}
</pre>
<p>On the PHP side, it's necessary to use <a href="http://us2.php.net/manual/en/function.json-decode.php">json_decode()</a> to convert the JSON $_POST parameter string (named "myJson") to an array:</p>
<pre class="brush: php;">
&lt;?php
$data = json_decode($_POST['myJson'], true);
print_r($data);
</pre>
<p>And the array is good to go:</p>
<pre class="brush: plain;">
Array
(
    [foo] =&gt; 123
    [bar] =&gt; 456
    [rows] =&gt; Array
        (
            [0] =&gt; Array
                (
                    [column1] =&gt; hello
                    [column2] =&gt; hola
                    [column3] =&gt; bonjour
                )

            [1] =&gt; Array
                (
                    [column1] =&gt; goodbye
                    [column2] =&gt; hasta luego
                    [column3] =&gt; au revoir
                )
        )

    [test1] =&gt; Array
        (
            [test2] =&gt; Array
                (
                    [test3] =&gt; baz
                )
        )
)
</pre>
<h2>Method 3 - JSON content-type</h2>
<p>My favorite method is to simply JSON stringify the JS array, and pass it as the AJAX request's body.  This requires that the content-type of the request be changed from the default "application/x-www-form-urlencoded" to "application/json", and that $.ajax()'s <a href="http://docs.jquery.com/Ajax/jQuery.ajax#options">processData</a> parameter be set to false so that the JSON data is not converted into a query string.  Also, the type of request must be POST, as the GET method does not support submitting data in the request body.</p>
<pre class="brush: jscript;">
sendAjax = function(){
	var data = {
				  foo:  123,
				  bar:  456,
				  rows: [
						  {
							column1 : 'hello',
							column2 : 'hola',
							column3 : 'bonjour',
						  },
						  {
							column1 : 'goodbye',
							column2 : 'hasta luego',
							column3 : 'au revoir',
						  },
						],
				  test1:{
						  test2: {
								   test3:  'baz'
								 }
						}
				};
	data = YAHOO.lang.JSON.stringify(data);

	$.ajax({
			type:           'post',
			cache:          false,
			url:            './ajax/',
			data:           data,
			processData:    false,
			contentType:   'application/json'
		   });
}
</pre>
<p>On the PHP side, it's necessary to manually read the JSON string directly from the request body using the <a href="http://php.net/manual/en/wrappers.php.php">php://input</a> stream.  The returned string can then be decoded into an array:</p>
<pre class="brush: php;">
&lt;?php
$data = file_get_contents('php://input');
$data = json_decode($data, true);
print_r($data);
</pre>
<p>Which gives us the array:</p>
<pre class="brush: plain;">
Array
(
    [foo] =&gt; 123
    [bar] =&gt; 456
    [rows] =&gt; Array
        (
            [0] =&gt; Array
                (
                    [column1] =&gt; hello
                    [column2] =&gt; hola
                    [column3] =&gt; bonjour
                )

            [1] =&gt; Array
                (
                    [column1] =&gt; goodbye
                    [column2] =&gt; hasta luego
                    [column3] =&gt; au revoir
                )
        )

    [test1] =&gt; Array
        (
            [test2] =&gt; Array
                (
                    [test3] =&gt; baz
                )
        )
)
</pre>
<p>Hopefully this tutorial has helped you get multidimensional array data passed to your webserver using AJAX.  If you know of another method, please submit it in the comments - thanks!</p>
<img src="http://www.zulius.com/blog/?ak_action=api_record_view&id=656&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.zulius.com/how-to/send-multidimensional-arrays-php-with-jquery-ajax/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
