douya7282 2018-08-03 08:02
浏览 122

jQuery自动完成,通过选择建议的选项填充多个字段

I have autocomplete jQuery script working for pulling suggestions from MySQL database when typing in form fields on the page (3 input fields). That is working fine, but what I would like is to when I select suggested option in the first field - all 3 fields should be filled. Fields that I have right now is first name, last name, and company. When I select the first name - last name and company should be automatically filled with data.

Here's php:

<html>
<head>
 <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">       
        <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="text/javascript">
$(function() 
{
 $( "#first_name" ).autocomplete({
  source: 'autocomplete.php'
 });
});
$(function() 
{
 $( "#last_name" ).autocomplete({
  source: 'autocomplete.php'
 });
});
$(function() 
{
 $( "#company" ).autocomplete({
  source: 'autocomplete.php'
 });
});
</script>
</head>
<body>
<div id="wrapper">

<div class="ui-widget">
 <p>First name</p>
 <input type="text" id="first_name">
</div>

<div class="ui-widget">
 <p>Last name</p>
 <input type="text" id="last_name">
</div>

<div class="ui-widget">
 <p>Company</p>
 <input type="text" id="company">
</div>

</div>
</body>
</html>

And here's the autocomplete.php file:

<?php

$host="localhost";
$username="user";
$password="password";
$databasename="dbname";

$connect=mysql_connect($host,$username,$password);
$db=mysql_select_db($databasename);

$searchTerm = $_GET['term'];

$select =mysql_query("SELECT * FROM jemployee WHERE first_name LIKE '%".$searchTerm."%'");
while ($row=mysql_fetch_array($select)) 
{
$spojeno = $row['first_name'] . ' ' . $row['last_name'] . ' ' . $row['kompanija'];
$data[] = $spojeno;
}
//return json data
echo json_encode($data);
?>

So, when the suggested option from "first_name" is selected - "last_name" and "company" should be filled with corresponding data from a database. Any suggestions?

  • 写回答

1条回答 默认 最新

  • doulubashang6936 2018-08-03 08:52
    关注

    Use something Jquery likes:

    $(document).on('keyup', '#firstname', funtion(){ $.ajax({ type:"POST", url:"ajax.php", data:$("#firstname").val(); }, success:function (res){ $("#lastname").val(res.lastname); $("#company").val(res.company); }, )}; });

    And PHP ajax.php file:

    <?php

    \\Select lastname, company with $_POST['data']

    echo json_endcode($result);

    Should check and handle the ajax response. If you can you this solution, please make it better.

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?