[英]Jquery autocomplete two fields based on one field using PHP and MySQL
I have a table in MySQL database with following columns: name, address, occupation, phone_number and serial_number. 我在MySQL数据库中有一个表,其中包含以下几列:名称,地址,职业,电话号码和序列号。 In my html form, among other fields, I have text fields for inputting Name, Phone number and Serial number.
在我的html表单中,除其他字段外,我还有用于输入名称,电话号码和序列号的文本字段。 How can I, using jQuery autocomplete and PHP/MySQL populate the Name and Phone number fields using the Serial number field?
如何使用jQuery自动完成功能和PHP / MySQL使用“序列号”字段填充“名称”和“电话号码”字段? I've managed to set autopopulate on one input field, but I don't know how to set it to populate the other two fields using the one field which has autocomplete.
我已经设法在一个输入字段上设置自动填充,但是我不知道如何使用具有自动完成功能的一个字段将其设置为填充其他两个字段。 Thanks
谢谢
these are the fields 这些是领域
<input type="text" name="name" id="autocomplete" />
<input type="text" name="phone_number" id="autocomplete" />
<input type="text" name="serial_number" id="autocomplete" />
this is my php script for getting the strings from db 这是我的php脚本,用于从db获取字符串
<?php
$dbhost = 'YOUR_SERVER';
$dbuser = 'YOUR_USERNAME';
$dbpass = 'YOUR_PASSWORD';
$dbname = 'YOUR_DATABASE_NAME';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
$return_arr = array();
if ($conn)
{
$fetch = mysql_query("SELECT name, phone_number, serial_number FROM users");
while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
$row_array['value'] = $row['name'] . " " . $row['phone_number'] " . $row['serial_number'] ";
array_push($return_arr,$row_array);
}
}
mysql_close($conn);
echo json_encode($return_arr);
?>
What you need to do is, when the autocomplete of the serial number is finished, run an ajax-call to retrieve the name and phone number based on the serial number and then populate those fields, something like this: 您需要做的是,当序列号的自动完成完成时,运行一个ajax调用以根据序列号检索名称和电话号码,然后填充这些字段,如下所示:
$('input[name=serial_number]').autocomplete({
onComplete: function (val) {
$.getJSON('/url-to-fetch-name-and-phone.php?serial_number=' + val, function (data) {
$('input[name=name]').val(data.name);
$('input[name=tel]').val(data.tel);
});
}
});
Obviously you'll need to change some function and variable names depending on your setup. 显然,您需要根据设置更改一些函数和变量名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.