Ajax Toggle(on/off) on-click Update

example 
Index :
<td>
    <a href="javascript:void(0)" data-toggle="tooltip"  onclick="toggleProductStaple({{ $item->id }},{{ $item->is_staple }})" >
        @if($item->is_staple === 1)
            <i data-toggle="tooltip" data-original-title="Active" class="fa fa-toggle-on text-inverse m-r-10"></i>
        @else
            <i data-toggle="tooltip" data-original-title="Inactive" class="fa fa-toggle-off text-inverse m-r-10"></i>
        @endif
    </a>
</td>
function:
<script>
    function toggleProductStaple(product_id, is_staple) {
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
            }
        });
        $.ajax({
            type: "post",
            url: BASE_URL + '/toggleProductStaple',
            data: {
                i_id: product_id,
                is_staple : is_staple
            },
            success: function (response) {
                location.reload();
            }
        });
    }
</script>
controller:
public function toggleProductStaple(Request $request) {    $product  = $this->_objProducts->getProductDetailByProductId($request->get('i_id'));    $product->is_staple = ($request->get('is_staple') == 1) ? 0 : 1;    $this->_objProducts->updateProductById(json_decode(json_encode($product), true));    return response()->json(['success' => 1]);
}

Popular posts from this blog

Yii Framework In Update Time View Image In Form