Need to get the value from the database and populate the dropdown using jquery

I need to populate a drop down list when I select a specific value and the parameters should be requested from the database.

Can I achieve this from jquery? if I can then please, I would appreciate any help ..

+3
source share
5 answers

You can do it with jQuery and AJAX

jQuery.post('dropdownValues.php', {parameterSentToServer1:'value', param2:'value2'}, function(data){jQuery('#mydropdown option').remove(); //Remove current options
for (var option in data.results){
    jQuery('#mydropdown').append('<option value="'+option.value+'">'+option.name+'</option>');
}}, 'json');

in dropdownValues.php you will need to build a json object with the result of the SQL query, the object must be in this format (for good work with the above script):

echo '{results:[{value:1, name:'Option1'}, {value:2, name:'Option2'}]};
+2
source

(, <select>)? , :

var data = ["Hello World!", 42, true, "Foo"];
var select = document.getElementById("myPopupMenu");

for (var i = 0, l = data.length; i < l; i++)
{
    var option = new Option();
    option.innerHTML = data[i];
    select.appendChild(option);
}

JavaScript- , jQuery.

0

. AJAX.

Ajax - , , , - javascript, . javascript Ajax, , ( Javascript ), ( - ).

Google , Ajax JQuery. , : http://www.ajaxlines.com/ajax/stuff/article/use_jquery_ajax_to_create_options_in_a_dropdown_menu.php

0

You can do this with this plugin without any problems. http://www.texotela.co.uk/code/jquery/select/

0
source

Note that adding each <option>in <select>will reload the DOM for each addition. This can become very slow if you have many options. It’s more efficient to build an integer <select>with its option in the line, and then replace the existing one.

0
source

Source: https://habr.com/ru/post/1707138/


All Articles