Reputation: 339
I have a project which I can add multiple data. ex: I will add 2 names and it will be display on the next table. Also I converted each data into json so that I can pass it to the controller. Now my problem is that every time I click the check button the controller can only read the last data I inputted. Is there any way to get all data from table and pass it to controller? Here's my script.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function(){
$("#submit-button").click(function(){
var data = $('#form_id').serializeArray();
var obj = {};
for (var i = 0, l = data.length; i < l; i++) {
obj[data[i].name] = data[i].value;
}
var json_text = JSON.stringify(obj);
$('#submit-button').after('<pre>' + json_text + '</pre>'); //shows only to determine if its working.
$('table.footable tbody').append('<tr><td><input type="hidden" name="request" value='+json_text+'></td> <td>'+obj['firstname']+'</td><td>'+obj['lastname']+'</td></tr>');
$("#firstname").val('');
$("#lastname").val('');
})
})
</script>
<form id="form_id">
<label for="firstname">First Name:</label>
<input type="text" id="firstname" name="firstname" value="" placeholder="Max" required="required" autofocus="autofocus" />
<label for="lastname">Last Name:</label>
<input type="text" id="lastname" name="lastname" value="" placeholder="Max" required="required" autofocus="autofocus" />
<input type="button" value="submit" id="submit-button" />
</form>
<div id="table">
<h2>List of all person</h2>
<form method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<table class="footable">
<thead>
<tr>
<th data-class="expand"> First Name </th>
<th> Last Name </th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<button class="btn btn-lg btn-primary btn-sm">Check</button>
</form>
</div>
</body>
</html>
Upvotes: 0
Views: 3788
Reputation: 339
Due to limited characters in comment. I will post my latest issue here.
I dont know if it is correct. Here's my latest code
Input example:
Firstname Lastname
Kerwin Manisan
Ivan Sanchez
Javascript
$('table.footable tbody').append('<tr><td><input type="text" name="firstname[]" value="'+obj['firstname']+'"></td><td><input type="text" name="lastname[]" value="'+obj['lastname']+'"></td></tr>');
Controller:
public function test(Request $request){
return $data = $request->input('firstname');
}
Result:
["Kerwin","Ivan"]
Sad to say the output that I need is something like this.
[{"firstname":"Kerwin","lastname":"Manisan"},{"firstname":"Ivan" , "lastname":"Sanchez"}]
EDIT: Problem solved. I used count to determine how many rows and then used loop to show the rows.
Controller:
public function test(Request $request){
$count = count($request->input('firstname'));
for ($i=0; $i<$count; $i++){
$data[] = array('firstname' => $request->input('firstname')[$i], 'lastname' => $request->input('lastname')[$i]);
}
return $data;
}
Upvotes: 0
Reputation: 2716
Instead of storing all the JSON data as text in the hidden field, create a new input for each new name. Instead you would add hidden inputs in your java script which would result in something like this
<input type="hidden" name="name[0]">
<input type="hidden" name="name[1]">
In your controller you can use the following (based on using request laravel5)
public function myFunction(Request $request) {
foreach($request['name'] as $name)
{
//do something with your name variable
}
}
Upvotes: 1