Problem with dynamic select mysql

Problem with dynamic select mysql

Hi, I have a problem, and I don't know if I'm writting in the correct place. First of all, I want to thank you so much for your time and for any help you can give me. Sorry if I've gone wrong about the place where it was suposed I should write in, and forgive my poor english too!

I'm in a project, and I want to create a dynamic selects, working with PHP and Mysql. I have no problem showing the first selects' options. I can obtain the value of the selected option. But I can't show the options in the second <select> taking into account the value I've got.

I think my problem is with the variable asignatura. When I run my application, the alert I've put in the 54 line (in adminFunctions.js) tells me "undefined". So I think my mainFunctions.inc.php doesn't recive the variable, or maybe the problem is in the PHP code...

The code I have is the following:

The page .html
  1. <tr>
  2.     <td>
  3.       Asignatura: <select name="selAsignatura" id="selAsignatura"></select>
  4.       Objetivo Etapa: <select name="selObjEtapa" id="selObjEtapa"></select>
  5.     </td>
  6.   </tr>
The adminFuntions.js, where I have two funtions: "selectAsignatura" (this one works fine) and "selectObjEtapa"

  1. jQuery.fn.selectAsignatura = function() {
  2.     $.ajax({
  3.               cache: false,
  4.               type: "POST",
  5.               dataType: "json",
  6.               url:"includes/appResponses.inc.php",
  7.               data:"accion=selectorAsignatura&id=" + Math.random(),
  8.               success: function(response){
  9.                         if(response.respuesta == true){
  10.                                  //alert('"Se muestran los valores de la BD');  
  11.                             //alert(response.contenido); 
  12.                             refDiv = document.getElementById('selAsignatura');
  13.                             refDiv.innerHTML = response.contenido;   

  14.                          }else{
  15.                                  alert("Ha habido algún error");
  16.                         }
  17.                },
  18.               error:function(){
  19.                   alert('ERROR AL MOSTRAR LAS ASIGNATURAS, INTENTE MAS TARDE');
  20.                   $('#ajaxLoaderRecuperar').addClass('hide');
  21.               }              
  22.           }); 
  23. };

  24. jQuery.fn.selectObjEtapa = function() {
  25.   $('#selAsignatura').change(function(){
  26.   var asignatura = $('#selAsignatura').serialize();
  27.   //alert(asignatura);
  28.   
  29.   // Función que envíe la var asignatura (id_asignatura) para buscar los objetivos que tengan esa id_asign por medio de una llmada AJAX...
  30.     $.ajax({
  31.        cache: false,
  32.        type: "POST", //Establecemos como se van a enviar los datos puede POST o GET
  33.        url: "includes/appResponses.inc.php", //SCRIPT que procesara los datos, establecer ruta relativa o absoluta
  34.        dataType: "json",
  35.        data: asignatura + "&accion=selectorObjEtapa&id=" + Math.random(), //Variable que transferira los datos
  36.        contentType: "application/x-www-form-urlencoded; charset=UTF-8", //Tipo de contenido que se enviara
  37.        success: function(response){
  38.                         if(response.respuesta == true){
  39.                                  //alert('"Se muestran los valores de la BD');  
  40.                             //alert(response.contenido); 
  41.                             refDiv = document.getElementById('selObjEtapa');
  42.                             refDiv.innerHTML = response.contenido;   

  43.                          }else{
  44.                                  alert("Ha habido algún error");
  45.                         }
  46.                },
  47.        error:function(response){
  48.                   // alert('ERROR AL MOSTRAR LOS OBJETIVOS DE ETAPA, INTENTE MAS TARDE');
  49.                   alert(response.mensaje);           
  50.                   $('#ajaxLoaderRecuperar').addClass('hide');
  51.               }
  52.      });
  53. });
  54. };
I run these functions, and both of them go to the file appResponses.inc.php, where we can see that:

  1. case 'selectorAsignatura':
  2.                 $appResponse['respuesta'] = true;
  3.                 $appResponse['mensaje'] = "Mostrando las Asignaturas";
  4.                 $appResponse['contenido'] = seleccionaAsignatura($mysqli);
  5. break;

  6. case 'selectorObjEtapa':
  7. if(seleccionaObjEtapa($_POST,$mysqli)){
  8. $appResponse['respuesta'] = true;
  9. $appResponse['mensaje'] = "Asignatura añadida correctamente";
  10. }else{
  11. $appResponse['mensaje'] = "No se ha podido guardar la asignatura";
  12. }
  13. break;
And, to finish, this one above runs in mainFunctions.inc.php (below) these functions:

  1. function seleccionaAsignatura($linkDB){

  2. $salida = '';

  3. $consulta = $linkDB -> query("SELECT id_asignatura,asignatura
  4.  FROM asignatura ORDER BY asignatura ASC");

  5. if($consulta -> num_rows != 0){
  6. // convertimos el objeto
  7. while($listadoOK = $consulta -> fetch_assoc())
  8. {
  9. $salida .= '
  10. <option value="'.$listadoOK['id_asignatura'].'">'.$listadoOK['asignatura'].'</option>
  11. ';
  12. }
  13. }
  14. else{
  15. $salida = '
  16. <option id="sinDatos" value="">NO HAY REGISTROS EN LA BASE DE DATOS</option>
  17. ';
  18. }

  19. return $salida;
  20. }

  21. // Función para extraer el listado de asignaturas
  22. function seleccionaObjEtapa($data,$dbLink){

  23. $salida = '';
  24. $asignatura = $_POST['asignatura'];
  25. $consulta = $linkDB -> query("SELECT id_obj_etapa,obj_etapa
  26.  FROM obj_etapa WHERE id_asignatura='$asignatura' ORDER BY id_obj_etapa ASC");

  27. if($consulta -> num_rows != 0){
  28. // convertimos el objeto
  29. while($listadoOK = $consulta -> fetch_assoc())
  30. {
  31. $salida .= '
  32. <option value="'.$listadoOK['id_obj_etapa'].'">'.$listadoOK['obj_etapa'].'</option>
  33. ';
  34. }
  35. }
  36. else{
  37. $salida = '
  38. <option id="sinDatos" value="">NO HAY REGISTROS EN LA BASE DE DATOS</option>
  39. ';
  40. }

  41. return $salida;
  42. }