[英]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.