I have a view where the user fills up a form and submits it into the database. The form has a table inside it whose rows are dynamically created. the user can add new rows and fill them up.
view.blade.php
<fieldset>
<div class="row">
<div class="col-xs-12">
<div id="LMOtablediv">
<input type="button" id="addmoreLMObutton" value="Add" onclick="insRow(event)" />
<table id="addLMOtable" border="1">
<thead>
<tr>
<td>No.</td>
<td>Name</td>
<td>Risk Level</td>
<td>Quantity</td>
<td>Volume</td>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>{!! Form::text('lmoname', null, array('id'=>'lmoname'))!!}</td>
<td>{!! Form::text('lmorisk_level', null, array('id'=>'lmorisk_level'))!!}</td>
<td>{!! Form::number('lmoquantity', null, array('id'=>'lmoquantity'))!!}</td>
<td>{!! Form::number('lmovolume', null, array('id'=>'lmovolume'))!!}</td>
<td></td>
<td><input type="button" id="delPOIbutton" value="Delete" onclick="deleteRow(this)"/></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</fieldset>
I am dynamically adding more rows to the table with javascript
script.js
function deleteRow(el) {
var table = document.getElementById('addLMOtable');
var i = el.parentNode.parentNode.rowIndex;
table.deleteRow(i);
while (table.rows[i]) {
updateRow(table.rows[i], i, false);
i++;
}
}
function insRow(event)
{
var table = document.getElementById('addLMOtable'),
tbody = table.getElementsByTagName('tbody')[0],
clone = tbody.rows[0].cloneNode(true);
var new_row = updateRow(clone, ++tbody.rows.length, true);
tbody.appendChild(new_row);
}
function updateRow(row, i, reset) {
row.cells[0].innerHTML = i;
var inp1 = row.cells[1].getElementsByTagName('input')[0];
var inp2 = row.cells[2].getElementsByTagName('input')[0];
var inp3 = row.cells[3].getElementsByTagName('input')[0];
var inp4 = row.cells[4].getElementsByTagName('input')[0];
inp1.id = 'lmoname' + i;
inp2.id = 'lmorisk_level' + i;
inp3.id = 'lmoquantity' + i;
inp4.id = 'lmovolume' + i;
if (reset) {
inp1.value = inp2.value = inp3.value = inp4.value = '';
}
return row;
}
controller file
public function create(Request $request)
{
$notification = new AddNotification;
$notification->lmoname = $request->lmoname;
$notification->lmorisk_level = $request->lmorisk_level;
$notification->lmoquantity = $request->lmoquantity;
$notification->lmovolume = $request->lmovolume;
$notification->user_id = Auth::user()->id;
$notification->save();
return redirect('home/notification/notificationForm');
// return view('ApplicationForms.notifi', $notification);
}
The Problem is when i add new rows, fill them up and submit, only the last row gets updated to the database. I know this is not the correct way to code the controller if i want to submit multiple rows at once. can please anyone guide on how to do it?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire