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.

5
  • 1
    JavaScript can't save anything to DB when being executed in browser. Commented Jul 16, 2019 at 7:16
  • you can do it with php (laravel) not javascript. Commented Jul 16, 2019 at 7: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 Commented Jul 16, 2019 at 7: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. Commented Jul 16, 2019 at 7:24
  • You can achieve that without JS, if you go with array input Commented Jul 16, 2019 at 7:29

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

expected ' , ' it says.
Where this errors appeared? Updated,check it again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.