3

I'm using Chosen.js to show multi select dropdown menus in my forms. All these selected data by a user goes to a mysql database in a single string separated by a ";". So far, so good.

Now my problem is, I want to give my users a edit form, where they can edit all there given data. So I have to read out the data from the database and shown this up on the form. But what is the right way for a multi select dropdown menu?

<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>

Data saved in Database like: "21;22;23".

I'm really stuck at this...

2 Answers 2

2

Get your data in array like

$selected_items= explode( ";", "21;22;23" );

then use in_array( $option_value, $selected_items ); to detect if that option is selected.

Sign up to request clarification or add additional context in comments.

1 Comment

Yea, thats it...this is so embarrassing. Bring the data back to an array and search for the values...thank you for this thought-provoking impulse.
0

Look into how you can- Normalize data

Till you do that, you can have a hack of splitting the string with ;

var data = "21;22;23"; var ids = data.split(';') will give you an array

of the ids ["21", "22", "23"]

Iterate over the ids and create your dropdown elements.

var html = '';
$.each(ids, function(id) {
   html += '<option value="' + id + "'>' + id + '</option>';
});

$('.some-dropdown').html(html);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.