Problem in updating user password through ajax in laravel 5.3

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 the moment that I am trying to do and change the password and when I click on the button change password opens a model that has two new password fields and re-enter password I already have ajax done and working together with controller of laravel that does the update in the database the problem and that I have two users and when I try to update the user password 2 it changes the user password 1 I do not know what the problem is with your help

User Listing Code

@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));
    } 

}
    
asked by anonymous 27.01.2017 / 12:37

1 answer

0

First, do not use &amp; inside Javascript, if it is a querystring per ajax only use & like this:

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

Or rather change to {} , so it will do the encode alone and do not need to use encodeURI :

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

Ajax with POST does not need cache: true

Ajax should look like this:

$.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 should take the script out of foreach , thus:

            @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>
    
27.01.2017 / 15:46