Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

342
Views
Ajax Laravel8: los campos de autocompletado en función del valor desplegable seleccionado no funcionan

Tengo una página de formulario simple donde quiero mostrar los detalles de un cliente en función del cliente seleccionado, soy un novato en ajax y he tenido este problema durante bastante tiempo y realmente no puedo hacer que funcione. .

Nota: "Nume" no es un error tipográfico, es la forma en que lo incluí en mi base de datos en lugar de nombre También: id = 'sel_depart' es la identificación del cliente seleccionado Y, name="sel_emp" id="sel_emp" es donde Quiero que se muestren sus detalles (que es "iban")

Este es mi controlador:

 public function getClientspay( Request $request, $id){ $clientsid = $request->clientsid; $clients = clients::select('*')->where('id', $clientsid)->get(); $response['data'] = $clients; return response()->json($response); }

Esta es mi ruta:

 Route::get('/getClientspay', [PlatiController::class,'getClientspay']);

Esta es la parte del formulario donde quiero mostrarlo:

 <div class="row"> <label for="">Nume Client</label> <label for="">Introduceti ID</label> <select id ='sel_depart' name='sel_depart' > @foreach ($clients as $clients) <option name='search' value="{{$clients->id}}">{{$clients->nume}}</option> @endforeach </select> <select name="sel_emp" id="sel_emp"> <option value="0"></option> </select>

Y esta es mi solicitud de ajax

 $('#sel_depart').change(function(){ // Department id var id = $(this).val(); // Empty the dropdown $('#sel_emp').find('option').not(':first').remove(); // AJAX request $.ajax({ url: 'getClientspay', type: 'get', data: {_token: CSRF_TOKEN, clientsid: clientsid}, dataType: 'json', success: function(response){ var len = 0; if(response['data'] != null){ len = response['data'].length; } if(len > 0){ // Read data and create <option > for(var i=0; i<len; i++){ var id = response['data'][i].id; var iban = response['data'][i].iban ; var option = "<option value='"+id+"'>"+iban+"</option>"; $("#sel_emp").append(option); } } } }); }); });
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Necesito más información. Abra las herramientas de desarrollo del navegador (generalmente F12) desde la página con el formulario y verifique las pestañas "Consola" y "Red" mientras cambia la selección. A partir de ahí, es posible comprender y solucionar su problema.

Editar: Gracias por publicar más información.

Como puedo ver, tienes un error en tu ajax. Está pasando una variable indefinida 'clinetsid'. Supongo que la variable debe contener la identificación del cliente, que es el valor que proviene de la selección. Ya está tomando ese valor y necesita pasarlo en datos ajax.

Déjame explicarte con código:

 $('#sel_depart').change(function(){ // Department id var id = $(this).val(); **//this is your select value (client id)** // Empty the dropdown $('#sel_emp').find('option').not(':first').remove(); // AJAX request $.ajax({ url: 'getClientspay', type: 'get', data: {_token: CSRF_TOKEN, clientsid: id}, **//Here you need to pass it** dataType: 'json', success: function(response){ var len = 0; if(response['data'] != null){ len = response['data'].length; } if(len > 0){ // Read data and create <option > for(var i=0; i<len; i++){ var id = response['data'][i].id; var iban = response['data'][i].iban ; var option = "<option value='"+id+"'>"+iban+"</option>"; $("#sel_emp").append(option); } } } }); }); });

Por favor acepte mi respuesta si es correcta.

over 4 years ago · Santiago Trujillo Report

0

 use App\Models\Client; public function getClientspay(Request $request, $id){ return response()->json(['client' => Client::where('id', $id)->first()]); // this will return specific user. }

Y elimine su bucle foreach para $clients dentro de su vista. Solo usa $cliente. Si está utilizando Blade y no necesita json, simplemente devuelva la variable $client a su vista.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!