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
- <tr>
- <td>
- Asignatura: <select name="selAsignatura" id="selAsignatura"></select>
- Objetivo Etapa: <select name="selObjEtapa" id="selObjEtapa"></select>
- </td>
- </tr>
The adminFuntions.js, where I have two funtions: "selectAsignatura" (this one works fine) and "selectObjEtapa"
- jQuery.fn.selectAsignatura = function() {
- $.ajax({
- cache: false,
- type: "POST",
- dataType: "json",
- url:"includes/appResponses.inc.php",
- data:"accion=selectorAsignatura&id=" + Math.random(),
- success: function(response){
- if(response.respuesta == true){
- //alert('"Se muestran los valores de la BD');
- //alert(response.contenido);
- refDiv = document.getElementById('selAsignatura');
- refDiv.innerHTML = response.contenido;
- }else{
- alert("Ha habido algún error");
- }
- },
- error:function(){
- alert('ERROR AL MOSTRAR LAS ASIGNATURAS, INTENTE MAS TARDE');
- $('#ajaxLoaderRecuperar').addClass('hide');
- }
- });
- };
- jQuery.fn.selectObjEtapa = function() {
- $('#selAsignatura').change(function(){
- var asignatura = $('#selAsignatura').serialize();
- //alert(asignatura);
-
- // 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...
- $.ajax({
- cache: false,
- type: "POST", //Establecemos como se van a enviar los datos puede POST o GET
- url: "includes/appResponses.inc.php", //SCRIPT que procesara los datos, establecer ruta relativa o absoluta
- dataType: "json",
- data: asignatura + "&accion=selectorObjEtapa&id=" + Math.random(), //Variable que transferira los datos
- contentType: "application/x-www-form-urlencoded; charset=UTF-8", //Tipo de contenido que se enviara
- success: function(response){
- if(response.respuesta == true){
- //alert('"Se muestran los valores de la BD');
- //alert(response.contenido);
- refDiv = document.getElementById('selObjEtapa');
- refDiv.innerHTML = response.contenido;
- }else{
- alert("Ha habido algún error");
- }
- },
- error:function(response){
- // alert('ERROR AL MOSTRAR LOS OBJETIVOS DE ETAPA, INTENTE MAS TARDE');
- alert(response.mensaje);
- $('#ajaxLoaderRecuperar').addClass('hide');
- }
- });
- });
- };
I run these functions, and both of them go to the file appResponses.inc.php, where we can see that:
- case 'selectorAsignatura':
- $appResponse['respuesta'] = true;
- $appResponse['mensaje'] = "Mostrando las Asignaturas";
- $appResponse['contenido'] = seleccionaAsignatura($mysqli);
- break;
- case 'selectorObjEtapa':
- if(seleccionaObjEtapa($_POST,$mysqli)){
- $appResponse['respuesta'] = true;
- $appResponse['mensaje'] = "Asignatura añadida correctamente";
- }else{
- $appResponse['mensaje'] = "No se ha podido guardar la asignatura";
- }
- break;
And, to finish, this one above runs in mainFunctions.inc.php (below) these functions:
- function seleccionaAsignatura($linkDB){
- $salida = '';
- $consulta = $linkDB -> query("SELECT id_asignatura,asignatura
- FROM asignatura ORDER BY asignatura ASC");
- if($consulta -> num_rows != 0){
-
- // convertimos el objeto
- while($listadoOK = $consulta -> fetch_assoc())
- {
- $salida .= '
- <option value="'.$listadoOK['id_asignatura'].'">'.$listadoOK['asignatura'].'</option>
- ';
- }
- }
- else{
- $salida = '
- <option id="sinDatos" value="">NO HAY REGISTROS EN LA BASE DE DATOS</option>
- ';
- }
- return $salida;
- }
- // Función para extraer el listado de asignaturas
- function seleccionaObjEtapa($data,$dbLink){
- $salida = '';
- $asignatura = $_POST['asignatura'];
- $consulta = $linkDB -> query("SELECT id_obj_etapa,obj_etapa
- FROM obj_etapa WHERE id_asignatura='$asignatura' ORDER BY id_obj_etapa ASC");
- if($consulta -> num_rows != 0){
-
- // convertimos el objeto
- while($listadoOK = $consulta -> fetch_assoc())
- {
- $salida .= '
- <option value="'.$listadoOK['id_obj_etapa'].'">'.$listadoOK['obj_etapa'].'</option>
- ';
- }
- }
- else{
- $salida = '
- <option id="sinDatos" value="">NO HAY REGISTROS EN LA BASE DE DATOS</option>
- ';
- }
- return $salida;
- }