简体   繁体   中英

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?

In the laravel controller I know how to save it.

I just want to know how is the function to pass the data from the view to 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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