I'm trying to implement a dynamic select box using jQuery and I have some problems... I have a 'route' to access to my DB and get the data of the new select box:
Route::get('contenidos/comboasign/{asignatura_id}', array('uses' => 'ContenidoController@comboAsignBloque'));
My Controller:
public function comboAsignBloque($asignatura_id)
{
if(Request::ajax()){
$bloques = DB::table('bloques')->where('asignatura_id','=', $asignatura_id)->orderBy('nombre', 'asc')->lists('id','nombre');
return json_encode(array("bloques" => $bloques));
}
}
And, in my View I have the form and this js:
$(document).ready(function(){
// Obtenemos los datos del selecbox1
$("#select_asignatura").change(function(e){
e.preventDefault();
var asignatura_id = $("#select_asignatura option:selected").val();
// AJAX request
$.get('comboasign/'+asignatura_id, function(data){
// #select_bloque is the select where I want to show my new options
var select = $('#select_bloque');
select.empty();
// select.html(data);
var optionsarray = data.split(',');
var seloption = " ";
$.each(optionsarray, function(i){
seloption += '<option value="' + optionsarray[i] + '">' + optionsarray[i] + '</option>';
select.append(seloption);
});
});
});
});
I need something in my 'js' to show the data in my new 'selectbox_bloque', or maybe I'm not doing good in my Controller... If I set my data in a normal , I get something like this:
{"bloques":{"bloque 1 mate":1,"Bloque 2 de Mate":6}}
I need to put this array into my select box and I can't find out how... I really appreciate any help.