简体   繁体   中英

Laravel 7 - Uncheck the checkbox with AJAX

I'm not really good at ajax. I want to uncheck the checkbox for the 'active' users.

  • I have this in my LoginController

 public function login()
        {
            $credentials = $this->validate(request(), [
                'email' => 'email|required|string',
                'password' => 'required|string',
            ]);
    

        if (Auth::attempt($credentials)) { //auth attemptdevuelve verdadero o falso en caso de que las credenciales correspondan o no
            //Inician cambios RDAN
            $user = Auth::user();
          if($user->active)  // checking your user is active or not
        {
            if ($user->hasRole('admin')) {
                return redirect('main');
            } else if ($user->hasRole('externo')) {
                return redirect('es/user_form');
            } else if ($user->hasRole('profesor')) {
                return redirect('main');
            } else if ($user->hasRole('registrador')) {
                return redirect('select_lang');
            } else {
                return back()->withErrors(['email' => 'Incorrect user permissions'])
                    ->withInput(request(['email']));
            }
          }
           else
          {
              Auth::logout(); //to logout user
        return back()->withErrors(['email' => 'Account Deactivated please contact admin'])->withInput(request(['email'])); // error message if user deactivated.
              }
            //Terminan cambios RDAN

        } else {
            return back()->withErrors(['email' => 'Incorrect user permissions'])
                ->withInput(request(['email']));
        }
    }
  • Index view:
 

     @foreach($users ?? '' as $user)   
          <tr>
             <td class="small  text-center">{{$user->name}}</td>
             <td class="small  text-center">{{$user->email}}</td>
             <td class="small  text-center">
             @foreach($user->roles as $role)
                {{$role->nombre_rol. '-'. ' '}}
             @endforeach
             </td>
    
             <td class="small  text-center">
                 <input {{$user->active=='1'?'checked':''}} type="checkbox" name="active" value="1">
              </td>
    
              <td class="small  text-center">
                  <div class="row">
                       <div >
                          <!--VIEW-->
                          <button onclick="verUsuario({{$user->id}})" class="btn btn-sm btn-outline-success m-1">
                           <i class="bi bi-eye align-content-center"></i>
                          </button>
                      </div>    
                       <div >
                           <!--EDIT-->
                           <button onclick="editarUsuario({{$user->id}})" class="btn btn-sm btn-outline-warning m-1">
                             <i class="bi bi-pencil-fill align-content-center"></i>
                            </button>
                            </div>
                               <!--Delete-->
                               <form id="delete"  action="{{url('Usuario/'.$user->id)}}" method="post">
                                {{csrf_field()}}
                                {{method_field('DELETE')}}
                                 <button type="submit" onclick="return confirm('Va a borrar la usuario {{$user->name}} ¿está seguro?')" class="btn btn-sm btn-outline-danger m-1"><i class="bi bi-trash-fill align-content-center"></i></button>
                             </form>
                          </div>
                      </td>
                  </tr>
          @endforeach

In MySQL database I added an 'active' column of type 'tinyint(1)' in the table 'users'. I added it in the view where the user 'admin' can enable or disable a user with a checkbox. I don't understand how to make sure that if it is not checked the value change for a 0. The checkbox is not in a form, it's in my index as a column. Can someone help me?

Try this if you are using tiny int.

<td class="small  text-center">
  <input {{$user->active == 1 ?'checked':''}} type="checkbox" name="active" value="1">
</td>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM