Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pkouadio's avatar

Dynamic SELECT don't work

Hey guys. I've been trying for three days to build a dynamic SELECT but I can't. Indeed, the two dropdowns are not automatically filled

My JS Code

<script>
$(document).ready(function () {
/*------------------------------------------
Diocese Dropdown Change Event
--------------------------------------------*/
$('#Diocese-dropdown').on('change', function () {
var  Id_Dioc = this.value;
$("#region-dropdown").html('');
$.ajax({
url:  "{{url('api/fetch-regions')}}",
type:  "POST",
data: {
diocese_id:  Id_Dioc,
_token:  '{{csrf_token()}}'
},
dataType:  'json',
success:  function (result) {
$('#region-dropdown').html('<option value="">-- Select region--</option>');
$.each(result.regions, function (key, value) {
$("#region-dropdown").append('<option value="' + value.id + '">' + value.Nom_Region + '</option>');
});
$('#section-dropdown').html('<option value="">-- Select section --</option>');
}
});
});
/*------------------------------------------
region Dropdown Change Event
--------------------------------------------*/
$('#region-dropdown').on('change', function () {
var  idregion = this.value;
$("#section-dropdown").html('');
$.ajax({
url:  "{{url('api/fetch-sections')}}",
type:  "POST",
data: {
region_id:  idregion,
_token:  '{{csrf_token()}}'
},
dataType:  'json',
success:  function (res) {
$('#section-dropdown').html('<option value="">-- Select section --</option>');
$.each(res.sections, function (key, value) {
$("#section-dropdown").append('<option value="' + value.id + '">' + value.Nom_Section + '</option>');
});
}
});
});
});
</script>

My Blade Code

<div  class="form-row">
    <select  id="Diocese-dropdown"  class="form-control">
	    <option  value="">-- Votre Diocese --</option>
		    @foreach ($dioceses as $data)
		    <option  value="{{$data->id}}"> {{$data->Nom_Diocese}}</option>
		    @endforeach
	</select>
</div>
<div  class="form-row">
	<select  id="region-dropdown"  class="form-control
	</select>
</div>
<div  class="form-row">
	<select  id="section-dropdown"  class="form-control"></select>
</div>

My Controller

<?php
namespace  App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Dioceses;
use App\Models\Regions;
use App\Models\Sections;

class  DropdownController  extends  Controller{

public  function  index(){
$data['dioceses'] = Dioceses::get(["Nom_Diocese", "id"]);
return  view('new-jeciste', $data);}

public  function  fetchRegions(Request  $request){
$data['regions'] = Regions::where("diocese_id", $request->diocese_id) ->get(["Nom_Region", "id"]);
return  response()->json($data);}

public  function  fetchSections(Request  $request){
$data['sections'] = Sections::where("region_id", $request->region_id)->get(["Nom_Section", "id"]);
return  response()->json($data);}
}

I tried a dd($data) in the controller to get an idea of what the form returns at the "api/fetch-regions" route but nothing there. Thank you for your help. After activating the first dropdown, the other two display "Nothing Selected".

0 likes
8 replies
Snapey's avatar

Check the console for errors

Check the network tools to a) see if a request is made, b) for the returned ajax response

Check the elements to see if anything changes.

pkouadio's avatar

Hi Snapey,

  • request type XHR is made with status 200
  • Nothing in the elements
Snapey's avatar

@pkouadio

request type XHR is made with status 200

to what URL?

What is the request body?

What is the response body?

pkouadio's avatar

@Snapey I changed browsers and found that there is no request.

I have an error.

Uncaught ReferenceError: $ is not defined
<anonymous>  http://localhost:8000/new-jeciste:300
pkouadio's avatar

@Snapey Hi I reloaded the jquery file. Here are the details of the request.

POST http://localhost:8000/api/fetch-regions
État 200 OK
Version	HTTP/1.1
Transfert	1,66 Ko (taille 566 o)
Politique de référent	strict-origin-when-cross-origin
Priorité de la requête	Highest

Here are the elements of the response

Access-Control-Allow-Origin *
Cache-Control	no-cache, private
Connection	close
Content-Type	application/json
Date	Mon, 05 Sep 2022 12:43:35 GMT
Date	Mon, 05 Sep 2022 12:43:35 GMT
Host	localhost:8000
Set-Cookie	XSRF TOKEN=eyJpdiI6IlJ5V2RZVFduMG5PQm5WRjZtOWZERGc9PSIsInZhbHVlIjoid01HMk1vS2djODFhcndKcGdZUjh3OEhjWVZUSmkxMHI3a0djbyt4ajJoYlV5bkFoRTBoVXVxOHhNdmw4VStyRVhmRkIvQUtNOVo2eHRCWFVCVFphbVYrNWNENG12ZjZhZURMQnpGOE9ZdDBzQm9BSnpJT1h2dzEwaXFrYXpHazAiLCJtYWMiOiJkNzgwMzcwNzIzYWUxOTQ5ODcyZTZiYmUyMGM3ZGU5YWNiZDE3ZjJhYmY2MGU4N2MwMzU4MDczN2E2ZWM3MTQ1IiwidGFnIjoiIn0%3D; expires=Mon, 05-Sep-2022 14:43:35 GMT; Max-Age=7200; path=/; samesite=lax
Set-Cookie	laravel_session=eyJpdiI6IlVGOHFCdVRyVnJLcEd4a2MwSWxYd0E9PSIsInZhbHVlIjoiU3B0WjY4ajlJeHF2ak9mVGtzTkQvMFNZU25tQWNTRElQd296cWpLSXQ0azlPd1FuK0RyL0cxVFcrL2dyN3RwWkI0VENQdE44R1VRVEZYTjVQYWd3bXBCY1VVRko1WTZWRTkyL2hBMElhSWlxWUN5ZWNackUvNjJrZ293ZXJXTnUiLCJtYWMiOiJjYTA1NTRlNDRmYWZlZmE5ZDY2NjU3MmU0OWFjZjllMTMyNTUwMjIyMWU1YzBmZjU2ZGM1NGRmOGJiNjBmNTAwIiwidGFnIjoiIn0%3D; expires=Mon, 05-Sep-2022 14:43:35 GMT; Max-Age=7200; path=/; httponly; samesite=lax
X-Powered-By	PHP/8.1.6
pkouadio's avatar

I have just seen that it returns a json but it does not display in the expected select.

{"regions":[
{"Nom_Region":"P\u00e8res Raymond Halter et Bernard Gouel","id":1},
{"Nom_Region":"Mgr Laurent YAPI","id":2},
{"Nom_Region":"Joseph Garnier et Pascal Acafou Grah","id":3},
{"Nom_Region":"P\u00e8re Pierre Michel PANGO","id":4},
{"Nom_Region":"Jean Baptiste Tilo","id":5},
{"Nom_Region":"Jacques Nomel et Blaise Anoh","id":6},
{"Nom_Region":"Ren\u00e9 N'Guio et Joseph Brou","id":7},
{"Nom_Region":"Nicolas Obo et Macaire Danho","id":8},
{"Nom_Region":"Mgr Albert ABLE et P\u00e8re Neuvry","id":9},
{"Nom_Region":"Mgr Joseph Akichi et R\u00e9gis PEILLON","id":10}
]}
pkouadio's avatar
pkouadio
OP
Best Answer
Level 1

Hi, the problem was due to the fact that bootstrap does not automatically refresh the fields. Thanks a lot

Please or to participate in this conversation.