I have a pretty complex html form that consist of 10+ normal input fields and also a dynamic row entry that creates an array of things. Imagine having a form with Personal details and the user can also add extra rows with family member details.
I need to be able to send both 'blocks' of data to a php. A simple $('#myForm').serialize();
wont do, because the 2nd block has to be sent as an array of things.
The problem that i am facing is this...
When i send the form values this way:
var frm_data = $('#myForm').serialize();
$.ajax({
"url": "request.php?action=save",
"data": frm_data,
"type": "POST",
"success": function(data) {
var response = JSON.parse(data);
if(response.data == "error"){
//
}else{
//
}
}
});
I get each input field from the form as an associate php array. Because the 2nd block with the dynamic rows has no unique name= values...(the only distinction is their <tr id=>
), i get only the last row's data.
Array
(
[name] => George Blah
[address] => Str. 25
[zip] => 35510
...
[extra_name] => Maria
[extra_Tele] => 21214454
[extra_Mobil] => 111111
[extra_Email] =>
)
But i managed to create a js array of all the 'extra' rows, before i send the form, and i have it stored in a variable, which i have available. The problem is, when i am sending BOTH (the form data and the 2nd block array) to the php file, the form's data is not an associated array anymore.
var extra = [];
//After a pretty complex function,
//i have all the extra dynamic rows stored in this array
var frm_data = $('#myForm').serialize();
$.ajax({
"url": "request.php?action=save",
"data": {"form": frm_data, "extras": JSON.stringify(extra)} ,
"type": "POST",
"success": function(data) {
var response = JSON.parse(data);
if(response.data == "error"){
//
}else{
//
}
}
});
And the result is like this:
Array
(
[form] => name=George+Blah&address=Str+25&zip=35510&=email=&extra_name=Maria&extra_Tele=21214454&extra_Mobil=111111&extra_Email=
[extras] => [{"extra_name":"Maria","extra_Tele":"21214454","extra_Mobil":"111111","extra_Email":""}]
)
I need the 'form' value to look like the 'extras' value, before it reaches the php file. I dont want to break the string sent to the php based on the '&' sign in case the user adds something like: 'George & Marry' for name, and all hell breaks lose after that.
So basically i want this:
[form] => name=George+Blah&address=Str+25&zip=35510&=email=&extra_name=Maria&extra_Tele=21214454&extra_Mobil=111111&extra_Email=
to be like this:
[form] => [{"name":"George Blah","address":"Str 25","zip":"35510","email":"".....}]
I hope i am understood. :/
Thanks