Many Rodríguez
Many Rodríguez

Reputation: 65

Laravel 7 - Uncheck the checkbox in AJAX

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

   

     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']));
            }
        }

 

  @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?

Upvotes: 0

Views: 270

Answers (2)

Yazan Fouad Aldbaisy
Yazan Fouad Aldbaisy

Reputation: 621

I think you forgot the round brackets in the condition

<input {{((int)$user->active==1)?'checked':''}} type="checkbox" name="active" value="1">

If not that is what you looking for and the active attribute is always false maybe you forgot to add the active attribute in the model User

Upvotes: 1

Julien Machin
Julien Machin

Reputation: 51

<element id="blabla" onclick="uncheck()" />

JS :

getElementById(blabla)
blabla.checked = true/false

PHP :

DB::table('tablename')->update(['checkbox' => 0/1]);

Also it would be preferable if you use a boolean instead of an integer because it's the real type you need and it's more simple, logic and maybe, secure

Upvotes: 0

Related Questions