dongxibo2095 2016-10-18 15:01
浏览 18
已采纳

名称输入PHP后自动完成表单

I found a couple of similar questions but nothing with the same "variables" so here we go:

I have a simple form where I enter a name and I need the rest of the form to be completed automatically (ID).

Fetching the data in the DB is not an issue, I'm able to return Json using PHP (Search.php):

//get search term
$searchTerm = $_GET['term'];

//get matched data from table
$query = $db->query("SELECT * FROM info_employer WHERE Prenom LIKE '%".$searchTerm."%' ORDER BY Prenom ASC");
while ($row = $query->fetch_assoc()) {
    $data[] = array( Nom => $row['Prenom'] . ' ' . $row['Nom'], Num => $row['Num'] );
}


//return json data
echo json_encode($data);

This give me an array that look like this:

[{"Nom":"Andre Nadeau","Num":"104J"},{"Nom":"Andre Potvin","Num":"130J"},{"Nom":"Andre Thibodeau","Num":"91J"}]

Friends told me that I should use Jquery and Ajax to use this array to fill my form but I have 2 issue.

First If I return an Array instead of "just the name" my autocomplete form don't work anymore. It give me X numbers of blank space (depending the number of results).

And of course my biggest problem is that i'm not able to send the ID (Num) in the form

Javascript i'm using to autocomplete the name :

    <script>
    $(function() {
        $( "#Nom" ).autocomplete({
            source: 'Search.php',
        })
    })
</script>
  • 写回答

1条回答 默认 最新

  • dthok9648 2016-10-18 15:05
    关注

    You need to change the return object to make it match the spec here

    $data[] = array( "label" => $row['Prenom'] . ' ' . $row['Nom'], "value" => $row['Num'] );
    

    This should result in an array of objects with a 'label' and 'value' key. This will work with your autocomplete.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集