$('#save').on("click", function() {
//collect the form data while iterating over the inputs
var data = [];
$('[name=form]').find('input, select, textarea').each(function() {
if ($(this).attr('value') != '') {
var name = $(this).attr('name');
var value = $(this).attr('value');
data[name] = value;
}
})
for (var i = 1, ii = form.length; i < ii; i++) {
var name = 'form' + i;
$('[name=' + name + ']').find('input, textarea').each(function() {
data[i] = [];
if ($(this).attr('value') != '') {
var name = $(this).attr('name');
var value = $(this).attr('value');
data[i][name] = value;
}
})
}
$.post('foo', data,
function(data) {
console.log(data);
I have the above jquery code where I wish to post multiple form data into my PHP backend controller.
In my controller, I used
$input = Input::all()
Question 1
How do I access the data in the array that I have passed? Let's say I have foo input and bar input in my form. I tried to use
$input['foo']
$input['bar']
but I'm getting internal server error with undefined index error.
Question 2
How do I access the data of the data[i][name]? Let's say I have form1, form2, form3, so it my data array will become
$data[1][foo]
$data[2][foo]
$data[3][foo]
How can I access these in my PHP controller?
Thanks and sorry for the long question