Autocomplete conditioned using JQuery

0

Greetings, I am developing a form where the user searches for a place and a work center using Input Text with Autocomplete in < strong> JQuery , but since there are many records in the database, I placed a Select that shortens the search. However, I do not know how to send the Select parameter to the PHP file where I search and autocomplete. I leave my code commented and in advance thank you very much.

JQuery

//Búsqueda de plazas
$(function() {
    $("#plaza").autocomplete({
        source: 'autocomplete.php',
    });
});   

//Búsqueda de centros de trabajo
$(function() {
    /$("#ct").autocomplete({
        source: 'autocompleteCT.php'
        });
    });

HTML

<label>Selecci&oacute;n de nivel educativo:</label>
            <br />
            <select class="md-input" name="nivelE" id="nivelE" class="md-input-bar" onchange="nivelEduc()"> <!--Aquí se manda a un Javascript que retorna a un hidden con el valor del nivel seleccionado-->
                    <option value="ShowAll" selected="selected">Seleccione nivel:</option>
                    <option value="Inicial">Educaci&oacute;n Inicial</option> <!--DDI -->
                    <option value="PreescolarGeneral">Preescolar General</option>
                    <option value="PrimariaGeneral">Primaria General</option>
                    <option value="EBA">Educaci&oacute;n b&aacute;sica para adultos</option>
                    <option value="SecundariasGenerales">Secundarias Generales</option>
                </select>
                <input type="hidden" id="nivel" name="nivel" />
            <br />
                <label>Buscar la plaza vacante:</label>
                <br />
                <input type="text" class="md-input" name="plaza" id="plaza" value="">
                <br />
                <label>Clave de Centro de Trabajo:</label>
                <input type="text" class="md-input" name="ct" id="ct" />

PHP Autocomplete

//Obtener el término buscado
    $searchTerm = $_GET['term'];
//Aquí pondría un switch o un if para hacer una consulta diferente, es  dependiendo de la opción del Select, es decir, si seleccionó preescolar, se consultarán sólo plazas y centros de trabajo de preescolar, etc. 
    $query = $db->query("SELECT * FROM analitico WHERE estatus='V' and plaza LIKE '%".$searchTerm."%' ORDER BY plaza ASC");
    while ($row = $query->fetch_assoc()) {
        $data[] = $row['plaza'];
    }


    //Regresar el Json
    echo json_encode($data);
    
asked by Fernanda Pichardo 27.09.2017 в 20:34
source

2 answers

1

I understand what you want to do. First, I commented that you do not need to save the selected value in your select in a hidden type input. Through JQuery you can get this value in the following way:

// Esta es una de las formas, existen otras
var nivel = $('#nivelE').find(':selected').val();

Then then in your autocomplete function you send this parameter in the following way:

JQuery

//Búsqueda de plazas
$(function () {
$("#plaza").autocomplete({
  source: function (request, response) {
    $.ajax({
      type: 'POST',
      url: 'autocomplete.php',
      dataType: 'json',
      data: {
        term: request.term,
        _nivel: nivel
      },
      success: function (data) {
        response(data);
      }
    });
  }
 });
}); 

If you set the POST send the variables that is why I capture them with $ _POST and not with $ _GET.

PHP SELF-COMPLETE

//Obtener el término buscado
$searchTerm = $_POST['term'];
$nivel = $_POST['_nivel'];

switch ($nivel) { 
  case "Inicial":
   //No se como se llama tu campo de nivel pero te dejo de ejemplo como 
   //sería la consulta
   $query = $db->query("SELECT * FROM analitico WHERE estatus='V' and plaza" 
   ."LIKE '%".$searchTerm."%' and nivelE = '".$nivel."' ORDER BY plaza ASC");                                                                                                             
  break;
  case "PreescolarGeneral":
      ....
  break;
  case "PrimariaGeneral":
     ....
  break;
  case "EBA":
    ...
  break;
  case "SecundariasGenerales":
   ...
  break;
 }

while ($row = $query->fetch_assoc()) {
    $data[] = $row['plaza'];
}


//Regresar el Json
echo json_encode($data);

I hope it serves you. Greetings.

    
answered by 28.09.2017 / 14:01
source
1

* Ajax *

function nivelEduc(){
    $nivelE = document.getElementById('nivelE').value;
    $.ajax({
    type: "POST",
    url: "autocompletado.php",
    data: "term=" + $nivelE,
    success: function(html){
        /* llenamos el Select */
    }
});
}
    
answered by 27.09.2017 в 21:37