Faiez
Faiez

Reputation: 305

Want to update active status from 1 to 0 using ajax

I am going to enabled or disable a button depending on the status of the value of column 'active' in database. And I want to change the button status using javascript or ajax. And 'active' value in database should also be updated.

blade.php

                                 <td class="text-center">
                                     <a href="#" data-toggle="modal" data-target="#myModal" class="edit"><i class="fa fa-comment"></i></a>
                                     @if( $joins->active == "1")
                                         <a href="{{route('asset.status',$joins->id)}}" id="return" data-toggle="modal" @if($joins->active === "1") class="edit  action-button" @else class="disabled2" @endif><i class="fa fa-get-pocket"></i></a>
                                     {{--@else--}}
                                     {{--<a href="#" id="return" data-toggle="modal" class="disabled2"><i class="fa fa-get-pocket"></i></a>--}}

                                     @endif
                                 </td>

script

<script>
    $(".action-button").click(function(){
        swal({
            type : 'warning',
            title: 'Submit what you want',
            input: 'text',
            showCancelButton: false,
            confirmButtonText: 'Submit',
            showLoaderOnConfirm: true,
            allowOutsideClick: false

        }).$.ajax({
        type: "POST",
        url : 'profile-management/employee-assets/update/{id}',
        data: { id: id },
        success: function(data){
            swal('Confirmed!','success')
        }

        });
    })

</script>

web.php

Route::get('profile-management/employee-assets/update/{id}','AssetController@statusUpdate')->name('asset.status');

Controller.php

   public function statusUpdate(Request $request,$id){
        $update = Asset::find($id);
        $update->active = "1";

        $update->save();
        $request->session()->flash('alert-info', 'Product Status Updated!');
        return redirect('/profile-management/employee-assets');
    }

Upvotes: 1

Views: 705

Answers (1)

Farhan Tahir
Farhan Tahir

Reputation: 2144

You could use only one button like blow:

Blade

<td class="text-center">
    <a href="#" data-toggle="modal" data-target="#myModal" class="edit"><i class="fa fa-comment"></i></a>
    <a href="{{route('asset.status',$joins->id)}}" id="return" data-toggle="modal" @if($joins->active === "1") class="edit  action-button" @else class="disabled2" @endif><i class="fa fa-get-pocket"></i></a>
</td>

PHP controller:

public function statusUpdate(Request $request,$id){
        $update = Asset::find($id);
        $update->active = !$update->active; // if true, it will give false

        $update->save();
        return \Resposne::json({status: true, active: $update->active});
    }

JavaScript:

Use the response in success callback:

<script>
    $(".action-button").click(function(){
        swal({
            type : 'warning',
            title: 'Submit what you want',
            input: 'text',
            showCancelButton: false,
            confirmButtonText: 'Submit',
            showLoaderOnConfirm: true,
            allowOutsideClick: false

        }).$.ajax({
        type: "POST",
        url : 'profile-management/employee-assets/update/{id}',
        data: { id: id },
        success: function(data){
            console.log(data,'data'); // {status: true, active: true/false }

         // Here use data to manipulate the button with id return, mean add/remove classes etc. d


            swal('Confirmed!','success')
        }

        });
    })

</script>

Upvotes: 1

Related Questions