The button .add-fields
will generate new name and address input using js.
The problem is I cannot get the values of inputs generated by the js
This is the original input from laravel blade.php
<input type="text" name="name[]" />
<input type="text" name="name[]" />
This is the javascript code which generates new inputs
$('.add-fields').on('click', add);
function add() {
var new_request_no = parseInt($('#total_request').val()) + 1;
var new_input = $("<input type='text' name='name[]'' /><input type='text' name='name[]'' />");
$('#new_request').after(new_input);
$('#total_request').val(new_request_no);
}
And this is from the controller
$data = array();
$name = $request->input('name');
$address = $request->input('address');
for ($i = 0; $i < count($name); $i++) {
$data[] = array(
'name' => $request->input('$name[$i]'),
'address' => $request->input('$address[$i]'),
);
}
$info = new InfoModel;
$info->data = json_encode($data);
$info->save();