Skip to content
Advertisement

PHP query response to populate HTML dropdown (loop thru 2D array)

I am getting exactly what I need from my PHP code and query; except I am having a really difficult time brining that data up to the front-end in order to populate an HTML dropdown.

Here is what I have on the PHP side; all working fine

JavaScript

Here is the jQuery; the response console log is only the flag (which) variable I am sending with the get request which determines which table to query via a user scenerio. the array I need is ommitted…

JavaScript

html drop down; just standard HTML select with a placeholder until populated:

JavaScript

what am I doing wrong here? or missing/forgetting?


Update: I am stuck on an error on the front-end, below is what console.log(response) outputs executed within my $.get. The data within the error log is correct; I just can’t seem to access it properly… 2D array issue?


JavaScript

Advertisement

Answer

Consider the following PHP.

JavaScript

This should then send back JSON Data of the Array.

Example

JavaScript

You can then use this in your Each Loop to build the Options.

JavaScript

You can also simplify your PHP Output so it’s just the result array of items.

JavaScript

Then you loop will also be simplified.

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