Show data from database in multiple dropdowns using PHP
I am using Chosen.js to display dropdown menus with multiple choices in my forms. All of this user-selected data enters the mysql database on a single line, separated by a ";" So far so good.
Now my problem is that I want to provide my users with an edit form where they can edit all the data. So I have to read data from the database and show it on the form. But what is the correct way for a dropdown menu with multiple choices?
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
Data stored in the database such as:. "21; 22; 23"
I'm really stuck with this ...
source to share
See how you can: Normalize data p>
Before that, you can hack the string with ;
var data = "21;22;23"; var ids = data.split(';')
will give you an array
identifiers ["21", "22", "23"]
Iterate over ids and create your dropdowns.
var html = '';
$.each(ids, function(id) {
html += '<option value="' + id + "'>' + id + '</option>';
});
$('.some-dropdown').html(html);
source to share