简体   繁体   English

如何将Laravel表中的多个复选框保存到数据库中?

[英]How can I save multiple checkbox in table for Laravel into database?

  1. How to create a Javascript function to save the selected checkbox into database when I click the Submit function? 单击提交功能时,如何创建一个Javascript函数将所选的复选框保存到数据库中?

In the laravel controller I know how to save it. 在laravel控制器中,我知道如何保存它。

I just want to know how is the function to pass the data from the view to javascript? 我只想知道函数如何将数据从视图传递到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>

I expect that if select all, all the data that has been selected will be saved in the database. 我希望如果全选,则所有已选择的数据都将保存在数据库中。

if single row data is selected, the data will be saved in the database. 如果选择单行数据,则数据将保存在数据库中。

in web file 在网络文件中

Route::post('checkedids','TestController@getCheckedIds')->name('postCheckedids');

then 然后

<td><input data-route="{{route('postCheckedids')}}" type="checkbox" class="singleSelectBox" name"singleBox" data-id="{{$item->id}}"></td>

the script code 脚本代码

     $('.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');
             }
         });
    });

then in your method in our controller 然后在您的方法中使用我们的控制器

public function getCheckedIds(Request $request){
$ids = $request->ids;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM