0
  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.

  • 1
    JavaScript can't save anything to DB when being executed in browser. – Tarasovych Jul 16 '19 at 07:16
  • you can do it with php (laravel) not javascript. – Kusy Jul 16 '19 at 07:18
  • Everytime when checkbox is change you have to call the ajax to laravel. In the laravel you can store the value in json format. So, ajax will check that ajax contain the checkbox value, if yes than so not need to store again or eles store it – Shivani Sonagara Jul 16 '19 at 07:24
  • @Tarasovych I know, i want to know how to code the javascript when you select all the checkbox for the data and when you click the submit button the data will pass into the controller and save into the database. – Muhd Zulhilmi Bin Abdullah Jul 16 '19 at 07:24
  • You can achieve that without JS, if you go with [array input](https://stackoverflow.com/questions/1978781/how-to-post-multiple-input-type-checkbox-as-array-in-php) – Tarasovych Jul 16 '19 at 07:29

1 Answers1

0

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;
}
mohamed hassan
  • 479
  • 3
  • 6