When a user selects a value from select_level dropdown box, a function returns an array of JSON values to be inserted into a multi select list. Currently the code only takes the first item in the array and adds it to the list, but I need to take all of the values from the array and add them to the list as different options. What is the best way to accomplish this?
Here is the code currently:
<script>
$(document).ready(function(){
$("#select_level<?php echo $id;?>").change(function(){
var level = $("#select_level<?php echo $id;?>").val();
var user_id = <?php echo $_REQUEST['user_id']?>;
$.ajax({
url:"./ajax_spdata.php?level="+level+"&user_id="+user_id+"&ajax=spdata",
dataType:"json"
}).success(function(data){
$("#spdata_level<?php echo $id;?>").html('<option value='+data[0]+'>'+data[0]+'</option>');
});
});
});
</script>
Here is what is executed in spdata.php
if($level == 0){
$spdata_array[] = "Value 1";
$spdata_array[] = "Value 2";
$spdata_array[] = "Value 3";
$spdata_array[] = "Value 4";
}
if($level == 1){
$spdata_array[] = "Value 5";
$spdata_array[] = "Value 6";
$spdata_array[] = "Value 7";
$spdata_array[] = "Value 8";
}
echo json_encode($spdata_array, JSON_FORCE_OBJECT);
Here is what gets returned if they select 1 from the select_level dropdown:
Object {1: "Value 1", 2: "Value 2", 3: "Value 3", 4: "Value 4"}