Skip to content
Advertisement

PHP – Add dropdown options from an array

I have an array

JavaScript
JavaScript

Then I’m looping through the result set and output the data

JavaScript

Output HTML should look like this:

JavaScript

What I need is for example the first column has three values in common, hno1, hno2 and hno3. I need to display these three values in all the dropdowns in the first column with the existing value($field) as preselected.

Thanks in advance.

Advertisement

Answer

Here is the full working code prototype – annotations explain everything.
Note: This will not post any values to a page or do something else as there are no <form> tags inside … at least it has unique ids for each select so it can be acessed via JS. Please mark the answer as accepted and upvote it if it is the desired result.

Update: 23.01 – 13:29 CET Inserted the code for the default selection of the element that is initially contained in the array.

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