[英]How can I save multiple checkbox in table for Laravel into database?
在laravel控制器中,我知道如何保存它。
我只想知道函數如何將數據從視圖傳遞到javascript?
``
<div class="table-responsive">
<button style="margin-bottom: 10px" class="btn btn-primary submitDatabase" data-url="">Submit to database</button>
<table class="table table-hover">
<thead>
<tr>
<th width="50px"><input type="checkbox" id="checkBox"></th>
<th>ID</th>
<th>Approved Date</th>
<th>Name</th>
<th>NRIC</th>
<th>Hospital</th>
<th>Condition</th>
<th>Cancer Type</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach($datas as $item)
<tr>
<td><input type="checkbox" class="singleSelectBox" data-id="{{$item->id}}"></td>
<td>{{$item->id}}</td>
<td>{{date('d-m-Y',strtotime($item->dtDecidedOn))}}</td>
<td>{{$item->varNm}}</td>
<td>{{$item->varIdNumber}}</td>
<td>{{$item->varHospitalNm}}</td>
<td>{{$item->varPrognosesNm}}</td>
<td>{{$item->varCancerNm}}</td>
<td><button class="btn btn-primary btn-sm reprint" id="{{$item->id}}" value="#">View</button></td>
</tr>
</tbody>
@endforeach
</table>
</div>
我希望如果全選,則所有已選擇的數據都將保存在數據庫中。
如果選擇單行數據,則數據將保存在數據庫中。
在網絡文件中
Route::post('checkedids','TestController@getCheckedIds')->name('postCheckedids');
然后
<td><input data-route="{{route('postCheckedids')}}" type="checkbox" class="singleSelectBox" name"singleBox" data-id="{{$item->id}}"></td>
腳本代碼
$('.singleSelectBox').change(function (e) {
var route = $(this).attr('data-route');
var checked = [];
$.each($("input[name='singleBox[]']:checked"), function(){
checked.push($(this).val());
});
if (checked.length==0){
return;
}
$.ajax({
type:'post',
url :route,
dateType:'json',
data:{checked:checked},
success: function (data){
if (data.status==true){
console.log(data)
}else{
}
},error:function(){
alert('error,try again');
}
});
});
然后在您的方法中使用我們的控制器
public function getCheckedIds(Request $request){
$ids = $request->ids;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.