Problem to update user password via ajax in Laravel 5.3

Asked

Viewed 57 times

0

I have a table where I list all the users I have in the database and I have a td where are the actions for them that contain two buttons load balance and change the password at this time I am trying to do and change the password i by clicking the change password button opens a model which has two new password fields and reintroduce password have already ajax done and working together with the controller of the Language that updates the database the problem and that I have two users and when I try to update the password of the user 2 it changes the password of the user 1 I do not know what the problem count on your help

Code of the User List

@extends('admin.index')
@section('title', 'Jogadores')
@section('content')
<div class="row">
    <div class="col-lg-12">
        <div class="content-box">
            <div class="head info-bg clearfix">
              <h5 class="content-title pull-left">Listagem de Jogadores</h5>
              <div class="functions-btns pull-right">
                <a class="refresh-btn" href="#"><i class="zmdi zmdi-refresh"></i></a>
                <a class="fullscreen-btn" href="#"><i class="zmdi zmdi-fullscreen"></i></a>
                <a class="close-btn" href="#"><i class="zmdi zmdi-close"></i></a>
              </div>
            </div>

            <div class="content">
                <div class="table-responsive alt-table">
                    <table class="table table-hover table-bordered">
                        <thead>
                            <tr>
                                <th class="table-check">
                                    ID do Jogador
                                </th>
                                <th>Nome do Jogador</th>
                                <th>Data do Registo</th>
                                <th>Ações</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($players as $listar_players)
                            <tr>
                                <td class="table-check">
                                    {{ $listar_players->id }}
                                </td>
                                <td class="table-check">
                                   {{ $listar_players->username }}
                                </td>
                                <td class="table-check">
                                    {{ $listar_players->created_at }}
                                </td>
                                <td class="table-check">
                                    <div class="btn-group">
                                        <button type="button" data-toggle="modal" data-target="#modal-add-credito" class="btn btn-success"><i class="fa fa-usd" aria-hidden="true"></i></button>
                                        <button type="button" data-toggle="modal" data-target="#modal-update-senha" class="btn btn-success"><i class="fa fa-key" aria-hidden="true"></i></button>
                                    </div>
                                    <div class="modal fade" id="modal-update-senha" tabindex="-1" role="dialog" aria-hidden="true">
                                        <div class="modal-dialog">
                                            <div class="modal-content">
                                              <div class="modal-header">
                                                  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                                  <h4 class="modal-title">Mudar Password</h4>
                                              </div>
                                              <div class="modal-body">
                                                <form action="alterarPassword" method="post" enctype="multipart/form-data" class="form-horizontal">
                                                    <div class="form-group">
                                                      <div class="col-sm-12">
                                                        <input type="password" class="form-control" id="password" name="password" placeholder="Nova Password">
                                                      </div>
                                                    </div>
                                                    <div class="form-group">
                                                      <div class="col-sm-12">
                                                        <input type="password" class="form-control" id="conf_password" name="conf_password" placeholder="Reintroduza nova password">
                                                      </div>
                                                    </div>
                                                </form>
                                              </div>
                                              <div class="modal-footer">
                                                  <button type="button" id="btn-update-pass-{{ $listar_players->id }}" class="btn btn-lg btn-info m-b-5">Guardar</button>
                                                  <button type="button" class="btn btn-lg btn-default m-b-5" data-dismiss="modal">Cancelar</button>
                                              </div>
                                            </div>
                                        </div>
                                    </div>
                                    <script>
                                        $(document.body).on('click', '#btn-update-pass-{{ $listar_players->id }}', function(e) {
                                            var password = $("#password").val();
                                            var conf_password = $("#conf_password").val();

                                            if(password === '' || conf_password === ''){
                                                toastr.error('Preencha os campos obrigatórios!');
                                            }else if(password != conf_password){
                                                toastr.error('Password não coincide');
                                            }else{

                                            e.preventDefault();

                                            var id_jogador = "{{ $listar_players->id }}";
                                            var password = $("input#password").val();

                                            var dataString = 'id_jogador='+id_jogador+'&amp;password='+password;

                                              $.ajax({
                                                    type: "POST",
                                                    url: "alterarPassword",
                                                    headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                                                    data: dataString,
                                                    cache: true
                                              }).done(function( msg ) {
                                                  toastr.success('Dados guardados com sucesso!');
                                              }).fail(function(data) {
                                                  toastr.error('Ocorreu um erro!');
                                              });  
                                            }
                                        });      
                                    </script>
                                </td>
                            </tr>
                            @endforeach 
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

Controller

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Jogadores;
use DB;
use Auth;
use Redirect;
use Hash;
use Response;
use Illuminate\Support\Facades\Input;

class AlterarSenhaJogadoresController extends Controller{

    public function update_senha_jogador (Request $request){

         $id_jogador = $request->input( 'id_jogador' );
         $password = Hash::make($request->input( 'password' ));

         $update_senha_jogador = DB::table('players')->where('id', '=', $id_jogador)->update(array('passwd' => $password));
    } 

}

1 answer

0

First, don’t use the &amp; inside Javascript, if it is a querystring by ajax use only the & thus:

 var dataString = 'id_jogador='+id_jogador+'&password='+password;

Or better switch to {}, so it will make the Meeting alone and need not use encodeURI:

var dataObj = {
    'id_jogador: id_jogador,
    'password': password
};

Ajax with POST, no need cache: true

It should look like Ajax:

$.ajax({
      type: "POST",
      url: "alterarPassword",
      headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
      data: dataObj
})

HTML should look like this:

<button type="button" class="btn-update-pass btn btn-lg btn-info m-b-5" data-id-jogador="{{ $listar_players->id }}">Guardar</button>

And you must take the script out of the foreach, being like this:

            @foreach($players as $listar_players)
            <tr>
                <td class="table-check">
                    {{ $listar_players->id }}
                </td>
                <td class="table-check">
                   {{ $listar_players->username }}
                </td>
                <td class="table-check">
                    {{ $listar_players->created_at }}
                </td>
                <td class="table-check">
                    <div class="btn-group">
                        <button type="button" data-toggle="modal" data-target="#modal-add-credito" class="btn btn-success"><i class="fa fa-usd" aria-hidden="true"></i></button>
                        <button type="button" data-toggle="modal" data-target="#modal-update-senha" class="btn btn-success"><i class="fa fa-key" aria-hidden="true"></i></button>
                    </div>
                    <div class="modal fade" id="modal-update-senha" tabindex="-1" role="dialog" aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content">
                              <div class="modal-header">
                                  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                  <h4 class="modal-title">Mudar Password</h4>
                              </div>
                              <div class="modal-body">
                                <form action="alterarPassword" method="post" enctype="multipart/form-data" class="form-horizontal">
                                    <div class="form-group">
                                      <div class="col-sm-12">
                                        <input type="password" class="form-control" id="password" name="password" placeholder="Nova Password">
                                      </div>
                                    </div>
                                    <div class="form-group">
                                      <div class="col-sm-12">
                                        <input type="password" class="form-control" id="conf_password" name="conf_password" placeholder="Reintroduza nova password">
                                      </div>
                                    </div>
                                </form>
                              </div>
                              <div class="modal-footer">
                                  <button type="button" class="btn-update-pass btn btn-lg btn-info m-b-5" data-id-jogador="{{ $listar_players->id }}">Guardar</button>
                                  <button type="button" class="btn btn-lg btn-default m-b-5" data-dismiss="modal">Cancelar</button>
                              </div>
                            </div>
                        </div>
                    </div>
                </td>
            </tr>
            @endforeach
            </tbody>
        </table>

      <script>
      $("body").on('click', '.btn-update-pass', function(e) {
          e.preventDefault();

          var password = $("#password").val();
          var conf_password = $("#conf_password").val();

          if(password === '' || conf_password === ''){
              toastr.error('Preencha os campos obrigatórios!');
          }else if(password != conf_password){
              toastr.error('Password não coincide');
          }else{
              var id_jogador = $(this).data("id-jogador"); //Pega o ID dinamicamente

              var dataObj = {
                  'id_jogador: id_jogador,
                  'password': password
              };

              var dataString = 'id_jogador='+id_jogador+'&amp;password='+password;

              $.ajax({
                  type: "POST",
                  url: "alterarPassword",
                  headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                  data: dataString
              }).done(function( msg ) {
                  toastr.success('Dados guardados com sucesso!');
              }).fail(function(data) {
                  toastr.error('Ocorreu um erro!');
              });
          }
      });
      </script>
    </div>
</div>
  • I did it all like I said in the question but it still does the same as getting id 1 when I try to change id 2

  • @Césarsousa when generating the page press Ctrl+U, it will open the view-source, then copy and paste in Pastebin and send me.

  • Don’t let me create in Pastebin says that Possible Spam Detected

  • I already got you here at this link http://pasted.co/400e7d50

  • I left the link already saw ?

  • @Césarsousa to try to discover the origin of the problem

  • OK thanks so much for the help

  • Found something, my friend ?

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.