Greetings, I am developing a form where the user searches for a vacancy and a job center through Input Text with an Autocomplete function in JQuery , but since there are many records in the database, I placed a Select that shortens the search. However, I don't know how to send the Select parameter to the PHP file where I do the search and autocomplete. I leave my commented code and thank you very much in advance.
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ó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ón Inicial</option> <!--DDI -->
<option value="PreescolarGeneral">Preescolar General</option>
<option value="PrimariaGeneral">Primaria General</option>
<option value="EBA">Educación bá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);
*Ajax*
I understand what you want to do. First I tell you that it is not necessary to save the selected value in your select in a hidden input. Using JQuery you can get this value in the following way:
So then in your autocomplete function you send this parameter to it in the following way:
JQuery
If you look, I send the variables by POST, that's why I capture them with $_POST and not with $_GET.
PHP AUTOCOMPLETE
I hope it helps you. Greetings.