Skip to content
Advertisement

Dynamic dropdown Ajax PHP request

I’m trying to make a dynamically populated drop down in a form for location selection. I have scoured stack in other posts that ask similar questions and some websites but my second dropdown is always blank.

The first dropdown is populated via a MySQL query.

Form section

JavaScript

Ajax request

JavaScript

ajax_county.php

JavaScript

Database.php

JavaScript

Before the district is changed. Before district is changed

After the district changed. After the district changed

A print screen from the Google Chrome console network tab (after the district select was changed) A print screen from the Google Chrome console network tab

A print of the console log The console log The request is sent and a response is recieved but the response is blank, the Content-Length: 0, there is nothing in it. Maybe there is something wrong with my php?

I’ve built my own MVC model and I have routing set up on my pages, would this affect the AJAX request?, i.e

JavaScript

Any ideas on how i can populate the dropdown

Advertisement

Answer

It seems to be a problem with the database. You can use mysqli_report(MYSQLI_REPORT_ALL); and see what is happening Check ajax_county.php the while at the end, it can create an infinite loop. You are using Database(), so maybe it is possible you can have a problem there.

Anyway, I prepared a code that should be work for you,

Please notice I’ve modified again your code for just for testing purposes (only using the getCouncils() function and the database query is a bit different)

index.php

JavaScript

Database.php

JavaScript

ajax_county.php

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement