I am calling a php function using ajax.
It works and i got a php array
in ajax success.
like.
Array ( [0] => Array ( [calendarId] => 1 [calendarName] => Calendar One )
[1] => Array ( [calendarId] => 2 [calendarName] => Calendar Second ) ) $.ajax({ url:"data.php", success:function(data){ console.log(data); } });
I want to set this array in to a drop-down.
how can I set this php array in to drop-down using jquery AJAX success event.
var li="";
$.ajax({
url:"data.php",
success:function(data){
console.log(data);
li+='<option>'+data[i].value+'</option>';
}
$('select').append(li);
HTML
<select id="yourSelect"></select>
SCRIPT
$(document).ready(function () {
var customArray = ["value1", "value2", "value3", "value4"];
var option;
for (i = 0; i < customArray.length; i++) {
option = $("<option/>", { value: customArray[i], html: customArray[i] });
$("#yourSelect").append(option);
}
});
var selOpts = "";
for (i=0;i<data.length;i++)
{
var id = data[i]['calendarId'];
var val = data[i]['calendarName'];
selOpts += "<option value='"+id+"'>"+val+"</option";
}
$('#selectDropdownSelector').append(selOpts);
You can create the options of the dropdowns from the array that you are getting in the following way
If type is JSON use data.d
success:function(data){
for (i = 0; i < data.d.length; i++) {
option = $("<option/>", { value: data.d[i], html: data.d[i] });
$("#selectList").append(option);
//OR
option.appendTo($("#selectList"))
}
}