【发布时间】:2017-02-16 15:57:11
【问题描述】:
我有这个代码:
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>
此代码向我显示每个 id 的值以自动完成 input,但它保存的是 name 而不是 id,我可以做些什么来显示值来帮助用户但保存id 在数据库中?
祝你有美好的一天!
更新代码:
现在向我显示我正在呼叫的名称列表,但是当我单击我想要的名称时,会自动替换为 id cliente_cod 的 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);
?>
【问题讨论】:
-
欢迎来到 Stack Overflow。请包含一些从您的 PHP 脚本返回的示例数据。它也可能有助于查看您的 JavaScript 代码。