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.

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

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?