Cannot POST a multi-array from javascript into a PHP
<p>I've searched through the forum but have not found any specific example relating to posting multi-arrays. Here's my problem:</p>

<p>I am doing the front end programming for an application my partner and I are writing. he has already done all the back end stuff but also created a working prototype front-end in PHP and HTML (circa 1991 style using tables for positioning... an absolute nightmare to read through).</p>

<p>I am re-coding everything from scratch using html/css/Jquery. I love jquery particulary for creating a dynamic form. In the use case, the customer is raising a new ticket (incident) with the provider related to a specific invoice. Since an invoice may contain dozens of line items, the customer can then also specify one or more items. In my code, I capture all of this in a multi-array which I then POST to a php page for processing. </p>

<p>My problem is that I cannot seem to find the right way to POST the data to the PHP from my JS code in a data structure it likes. </p>

<p>Onto the code: </p>

<h1><strong>Form Page</strong></h1>

<hr>

<p>Each item the user enters in has a few fields. I wrap each one in a fieldset. </p>

<pre><code>$('.add_product').click(function(){
                    index++;
                    $(this).parent().parent().append("&lt;fieldset&gt;" +
                    "&lt;label for='name'&gt;Name&lt;/label&gt;&lt;input type='text' alt='name' value='' id='" + this + "_name" + index + "'/&gt;" +
                     "&lt;label for='code'&gt;Code&lt;/label&gt;&lt;input type='text' alt='code' value='' id='" + this + "_code" + index + "'/&gt;" +
                     "&lt;label for='lotnumber'&gt;Lot&lt;/label&gt;&lt;input type='text' alt='lotnumber' value='' id='" + this + "_lotnumber" + index + "'/&gt;" +
                     "&lt;label for='expiration'&gt;Expiration&lt;/label&gt;&lt;input type='text' alt='expiration' value='' id='" + this + "_expiration" + index + "'/&gt;" +
                     "&lt;p class='remove_product'&gt;Remove&lt;/p&gt;&lt;/fieldset&gt;");
</code></pre>

<p>When the form is submitted, I select all the fieldsets into a multi-array like this: </p>

<pre><code> var postValues={};
 var i=0;
                $('fieldset').each(function(){

                    postValues[i]={"VarChar08":"","VarChar07": $(this).parent().parent().attr('id'),"VarChar14":"P" , "VarChar09": $(this).find('input[alt="name"]').val(), "VarChar10": $(this).find('input[alt="code"]').val(), "VarChar11": $(this).find('input[alt="lotnumber"]').val(), "VarChar12": $(this).find('input[alt="expiration"]').val(),"VarChar13":"","VarChar20":"1"} ;
                    i++;

                });
</code></pre>

<p>My last attempt was to JSON stringify the array: </p>

<pre><code>var itemValues= JSON.stringify(postValues);
</code></pre>

<p>then I post them to the php processor page:</p>

<pre><code> $.post("some.php", {items: itemValues}); 
</code></pre>

<h1><strong>PHP Processor Page</strong></h1>

<hr>

<pre><code>$itemValues=json_decode($_POST['items']);
$items = $itemValues;

// read into POST values for submitting to a stored procedure
foreach ($items AS $m =&gt; $tmp_items){
        $_POST['VarChar09'] = $items[$m][3]; // name
        $_POST['VarChar10'] = $items[$m][4]; // code
        $_POST['VarChar11'] = $items[$m][5]; // lot
        $_POST['VarChar12'] = $items[$m][6]; // expiry
}
</code></pre>

<p>Unfortunately this isnt working. The PHP processor page is not reading the data structure properly and therefore the values don't get assigned properly. I'm afraid i've reached the limits of my experience. I suspect its possible there is a flaw in my understanding of the data structure from the javascript side. I haven't yet been able to get my head around the whole idea of having an object of arrays, an object of objects, an array of objects or an array of arrays. Though again, this may or may not have anything to do with it. </p>

<p>Any help is much appreciated!</p>
