Fill a select via jquery in cakephp

1

I made it a little easier, but it still is not working. Instead of mounting everything in the javascript, in my action I mount all html and in javascript I only pass the html mounted to my select.

function MostraPrograma(){
 var _valorSelect = document.getElementById("CursoEscolaId").value;     
 $.post('/novas/mostra_programa/'+_valorSelect),$("#CursoNewAddForm").serialize(), function(data){                          
    $("#rowIdChata").html(data);

 }

}

and this is my view:

<?php
 if($info_campus == 0){
echo 'Não existem tipos de curso para esta escola';   
}else{
foreach($info_campus as $info){?>
<option value="<?=$info['CursoCampus']['id']?>"><?=$info['CursoCampus']['nome']?></option><?  php }} ?>
    
asked by anonymous 04.01.2016 / 17:01

1 answer

0

Your application will need to perform two operations:

1) Do the parse of the string in JSON format for javascript, there are several forms on the internet, either with pure Javascript or with libraries, but I do so:

Obs. I changed $.post by $.get I think it's this method you need.

     $.get('/novas/mostra_programa/'+_a, function(data){
         var opcoes = (new Function("return " + data +";"))();
     });

2) Loop and mount options

   $.get('/novas/mostra_programa/'+_a, function(data){
      var opcoes = (new Function("return " + data +";"))();

      var sel = document.getElementById('{ID DO SELECT}');
      var fragment = document.createDocumentFragment();

      for(var i = 0; i < opcoes.length; i++){
        var opt = document.createElement('option');
        opt.innerHTML = opcoes[i].CursoCampus.nome;
        opt.value = opcoes[i].CursoCampus.id;
        fragment.appendChild(opt);
     }

      sel.appendChild(fragment.cloneNode(true));
  });
    
04.01.2016 / 18:49