I have this code:
buscarcliente.php
<?php
include("../../conexion.php");
$nombre=$_GET["term"];
$sql_cliente="SELECT cod, nombre FROM rc_clientes WHERE nombre LIKE '%$nombre%'";
$buscar = mysqli_query($conexion,$sql_cliente);
$json=array();
while($row = mysqli_fetch_array($buscar)) {
array_push($json, $row['nombre']);
}
echo json_encode($json);
?>
renta_form.php JQuery:
$(function() {
$( "#cliente" ).autocomplete({
source: 'modulos/inspeccion/buscarcliente.php'
});
});
renta_form.php HTML:
<tr>
<td><b>Cliente:</b><br><input id="cliente" name="cliente" type="text"></td>
</tr>
This code shows me the values from each id to autocomplete an input, but it is saving the name and not the id, what can I do to show the value to help the user but save id in database?
Have a good day!
UPDATED CODE:
Now is showing me the list of the names that I am calling, but when I click the name that I want, is replace automatically by the cod of the id cliente_cod
HTML:
<tr>
<td><b>Cliente:</b><br><input id="cliente" name="cliente" type="text"></td>
<input id="cliente_cod" name="cliente_cod" type="hidden">
</tr>
JavaScript:
$(function() {
$( "#cliente" ).autocomplete({
source: 'modulos/renta/buscarcliente.php',
select: function(event, ui)
{
$("#cliente").val(ui.item.label);
$("#cliente_cod").val(ui.item.value);
}
});
});
PHP
<?php
include("../../conexion.php");
$nombre = $_GET["term"];
$sql_cliente="SELECT cod, nombre FROM rc_clientes WHERE nombre LIKE '%$nombre%'";
$buscar = mysqli_query($conexion,$sql_cliente);
$json=array();
while($row = mysqli_fetch_array($buscar)) {
array_push($json, array('value' => $row['cod'], 'label' => $row['nombre']));
}
header('Content-Type: application/json');
echo json_encode($json);
?>