0

I've searched through the forum but have not found any specific example relating to posting multi-arrays. Here's my problem:

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).

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.

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.

Onto the code:

Form Page


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

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

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

 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++;

                });

My last attempt was to JSON stringify the array:

var itemValues= JSON.stringify(postValues);

then I post them to the php processor page:

 $.post("some.php", {items: itemValues}); 

PHP Processor Page


$itemValues=json_decode($_POST['items']);
$items = $itemValues;

// read into POST values for submitting to a stored procedure
foreach ($items AS $m => $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
}

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.

Any help is much appreciated!

4
  • I suggest print_r($_POST); or similar to help debug the structure you're getting sent. Also, why are you writing into the $_POST variable? Commented Jan 6, 2013 at 22:06
  • After a quick look at the docs, have you tried something like {'items[]': itemValues} ? Commented Jan 6, 2013 at 22:23
  • certainly can't concatenate this which is a DOM element into the form html string not sure what you want there Commented Jan 6, 2013 at 22:48
  • @therefromhere That part is from my partners code, which sucks and I haven't gotten around to re-doing :) Commented Jan 8, 2013 at 0:53

1 Answer 1

0

Your input elements have sevral problems. First this is a DOM eleemnt that you are trying to concatenate into html string.

input elements have no name which is required for field to submit and create the key for $_POST

With a name attribute for element you can use serialize() to generate all the form data rather than hard coding the data object yourself.

var data=$('#formID').serialize();

 $.post("some.php", data, function(response){
     /* ajax success... do something with response if needed*/
 }); 

API reference: http://api.jquery.com/serialize/

Now your php $_POST keys will match names in fields

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks I'm gonna try your suggestion to use serialize() but a couple of questions: 1. Since i'm adding fieldsets dynamically, do I have to append an index number to the value of the name attribute? and 2. I'm not sure what you mean by the concatenation thing. I haven't had any problems assigning the values in the form fields to the key value pairs in my arrays.
This is what I get from a console.log on JSON.stringify(postValues);` using dummy values I put in {"0":{"VarChar08":"","VarChar07":"","VarChar14":"P","VarChar09":"test1","VarChar10":"12345","VarChar11":"67890","VarChar12":"12-12-12","VarChar13":"","VarChar20":"1"},"1":{"VarChar08":"","VarChar07":"","VarChar14":"P","VarChar09":"test2","VarChar10":"12345","VarChar11":"67890","VarChar12":"12-12-12","VarChar13":"","VarChar20":"1"},"2":{"VarChar08":"","VarChar07":"","VarChar14":"P","VarChar09":"test3","VarChar10":"12345","VarChar11":"67890","VarChar12":"12-12-12","VarChar13":"","VarChar20":"1"}}
isn't that json what you expected?
console.log of JSON object: {"0":{"VarChar08":"","VarChar07":"PRO.Estado","VarChar14":"P","VarChar09":"asaf","VarChar10":"12345","VarChar11":"67890","VarChar12":"12-12-12","VarChar13":"","VarChar20":"1"},"1":{"VarChar08":"","VarChar07":"PRO.Formato","VarChar14":"P","VarChar09":"asaf","VarChar10":"12345","VarChar11":"67890","VarChar12":"12-12-12","VarChar13":"","VarChar20":"1"},"2":{"VarChar08":"","VarChar07":"PRO.Formato","VarChar14":"P","VarChar09":"asaf","VarChar10":"12345","VarChar11":"67890","VarChar12":"12-12-12","VarChar13":"","VarChar20":"1"}}
print_r of json_decode($_POST['items']): stdClass Object ( [0] => stdClass Object ( [VarChar08] => [VarChar07] => EMB.Estado [VarChar14] => P [VarChar09] => test [VarChar10] => 12345 [VarChar11] => 67890 [VarChar12] => 12-12-12 [VarChar13] => [VarChar20] => 1 ) [1] => stdClass Object ( [VarChar08] => [VarChar07] => EMB.Plaga [VarChar14] => P [VarChar09] => test [VarChar10] => 12345 [VarChar11] => 67890 [VarChar12] => 12-12-12 [VarChar13] => [VarChar20] => 1 ) )
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.