dimanche 8 novembre 2020

Laravel/PHP/Ajax Update Checkbox without refreshing page

I would like to learn how to update a form component like Checkbox without having to refresh the page in laravel.

blade.php

<form id="canview">
  <fieldset>
   <div class="checkbox checkbox-success">
   <input type="checkbox" id="canViewBox" onChange="this.form.submit()" @if($document->pivot->canview == 1) checked @endif>
   <label for="canViewBox"></label>
   </div>
  </fieldset>
</form>
                                    
  <script type="text/javascript">
    $('#canview').on('submit',function(event){
    event.preventDefault();
    $.ajax({
    url: "/switchme",
    type:"POST",
    data:{
    "_token": "",
    },
    success:function(response){
     console.log(response);
    },
    });
    });
    </script>

The canDo route is simply switching the value between 0 and 1, I don't have real functionality atm, just testing it out to see how it works.




Aucun commentaire:

Enregistrer un commentaire