mox_mn

How to check is checkbox is selected and post it's data using jquery in laravel

Posted 2 years ago by mox_mn

I have a form which contain a checkbox in it. I need to check it's value from db , if it's true then it should be mark , else it should be unmark. problem here is i want to update database field whenever user clicks on this checkbox and change it.

This is my blade :

@extends('layouts.app')

@section('content')
    <div class="container">
        <div class="row">
            <div class="col-md-8 col-md-offset-2">
                <div class="panel panel-default">
                    <div class="panel-heading">settings</div>
                    <div class="panel-body">
                        <form role="form" method="POST" name="form"
                              action="{{ url('websites/webTargeting/'.$seat_id) }}">
                            {{ csrf_field() }}
                            <div class="box-shadow back-white padding">
                                <span class="description_text">webtargeting</span>
                                @for($i=0;$i<count($categories);$i++)
                                    <strong>{{ trans('objects.'.$categories[$i]) }} @if(count($categories)>0 && $i>=0 && $i<count($categories)-1){{ "," }} @endif</strong>
                                @endfor
                                <br>
                                <div class="margin-top"><input type="checkbox" id="webTargeting" name="webtargeting" value="">
                                    <div id="alert-box"><p id="alert-webtargeting" style="<?php if($activate == 'true'){ echo "color:green;"; } else { echo "color:red;"; }?>">@if($activate == 'true') {{ "web targeting is enabled." }} @else {{ "web targeting is disabled" }}@endif</p></div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    </div>
    </div>
    <script>
        $(document).ready(function () {

           $("#webTargeting").change(function () {
               if ($("#webTargeting").is(':checked')) {
                   $("#webTargeting").prop('value', true);
                   $post("websites/webTargeting/+<?php echo $seat_id; ?>" ,{
                    seat_id : "<?php echo $seat_id; ?>"
                   }
                   ,
                   function (response){
                    alert('enabled');
                   });
               }
               else
               if ($("#webTargeting").is(':unchecked')) {
                   $("#webTargeting").prop('value', false);
                   $post("websites/webTargeting/+<?php echo $seat_id; ?>" ,{
                    seat_id : "<?php echo $seat_id; ?>"
                   }
                   ,
                   function (response){
                    alert('disabled');
                   });
               }
           });
        });
    </script>
@endsection

Please sign in or create an account to participate in this conversation.