1

I want to toggle a boolean value in my table with a checkbox through jquery and ajax.

So whenever a user ticks the the checkbox it should toggle the value in table.

So far i came up with this but i need help:

$(document).ready(function(){
        $("input:checkbox").change(function() { 
            var isChecked = $("input:checkbox").is(":checked") ? 1:0; 
            $.ajax({
                type:'POST',
                url:'/activation',
                headers: {'X-CSRF-TOKEN': '{{ csrf_token() }}' },
                data: $('.checkbox').serialize(),
                    success:function(data){

                    }
            });
        });
    });
5
  • What exactly is not working? So far you've only shown how you do an ajax... Commented Oct 24, 2016 at 8:13
  • true...it's not working at all, i don't know what to put in my controller method si if that checkbox was 0 it should change the value in DB table and vice versa Commented Oct 24, 2016 at 8:14
  • Welp, if it's a 0(or a 1 for that matter) then Table::where('someField', 'someValue')->update(['someField' => 'someValue']). Have a look at their docs, if you have specific questions, me and probably everybody else here, would be more than happy to help. Commented Oct 24, 2016 at 8:17
  • OK i'll try to write the question better Commented Oct 24, 2016 at 8:18
  • @Andrew i did it....maybe you could tell me if i can make it better Commented Oct 24, 2016 at 12:11

3 Answers 3

10

@andre's answer is correct but

    if($user->active == 1){
        $user->active = 0;
    } else {
        $user->active = 1;
    }

this part can be done by a single line

$user->active = !$user->active;
Sign up to request clarification or add additional context in comments.

Comments

5

The only thing I'd modify would be returning an answer to the ajax call to let it know what's happening.

public function activation(Request $request)
{

    $user = User::findOrFail($request->user_id);

    if($user->active == 1){
        $user->active = 0;
    } else {
        $user->active = 1;
    }

    return response()->json([
      'data' => [
        'success' => $user->save(),
      ]
    ]);
}

And now in the front-end part:

$(document).ready(function(){
  $("input:checkbox").change(function() {
    var user_id = $(this).closest('tr').attr('id');

    $.ajax({
            type:'POST',
            url:'/activation',
            headers: {'X-CSRF-TOKEN': '{{ csrf_token() }}' },
            data: { "user_id" : user_id },
            success: function(data){
              if(data.data.success){
                //do something
              }
            }
        });
    });
});

jQuery Ajax documentation

8 Comments

I have a prepared success flash message so how can i use that....i have putted it in my answer
I'm sorry, see what exactly?
this part is what i need in your answer if(data.success){ //do something }
No, definitely not. That's the point of ajax, no reload. Do a console.log(data) to see what's in there. Learn to debug, it's a very good skill to have.
yes i understand now....but how to make some feedback for user...what to put here: if(data.success){ //do something }
|
-1

in model create a method

function toggleActive ()
{

  $this->active!=(int)$this->active;
  return $this;
}

in controller


$model->toggleActive()->save();

Comments

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.