使jQuery UI自动完成源依赖于另一个自动完成

时间:2015-05-19 11:49:57

标签: php jquery ajax jquery-ui jquery-ui-autocomplete

问题在于标题,我不知道如何使自动完成小部件依赖于另一个。在我的示例中,我的数据库中有两列:

  • 的firstName
  • lastName的

还有一些行:

Dupont Francois
Dupont Oliver
Lapeche Jean 

如果我在第一个自动填充输入中输入Dupont,我希望第二个自动填充功能建议OliverFrancois不是Jean

这是我的代码HTML:

<form action="../../controler/add/addUserDevice.php" onsubmit="return verifUserDevice(this)" method="post">
<table>
    <tr>
        <td>Last Name</td> 
        <td><input type="text" class="userLastName" name="userLastName"/></td>
    </tr>
    <tr>
        <td>First Name</td> 
        <td><input type="text" class="userFirstName" name="userFirstName" /</td>    
    </tr>
    <tr>
        <td colspan="3"><input class="buttonIndex" type="submit" value="Create" /></td>
    </tr>
</table>

我的seconde自动完成的PHP代码

<?php
include('../../model/connexion_sql.php');

if ((include_once '../../model/connexion_sql.php') === FALSE)
    exit('erreur include');

if (isset($_GET['term'])) {
    $connection = new Connection;
    $connection->connection();
    $conn    = $connection->getConnection();
    $requete = $conn->prepare('SELECT userFirstName FROM user WHERE userFirstName LIKE :term');
    $requete->execute(array('term' => $_GET['term'] . '%'));
    $array = array();
    while ($donnee = $requete->fetch()) {
        $array[] = $donnee['userFirstName'];
    }
    echo json_encode($array);
}
?>

和我的JavaScript:

$(function(){ 
    $(".userFirstName").autocomplete({
        source: '../../model/autocomplete/userFirstName.php',
        minLength: 1
    });
});

1 个答案:

答案 0 :(得分:0)

<itemWebApiGetFields>
        <!--<processor type="Sitecore.ItemWebApi.Pipelines.GetFields.GetFields, Sitecore.ItemWebApi" />-->
</itemWebApiGetFields>

您的ajax请求将如此。

$(function() { $(".userFirstName").autocomplete({ autoFocus:true, source: function( request, response ) { $.ajax({ url: '../../model/autocomplete/userFirstName.php?userLastName=' + $("#userLastName").val() + "&term=" + request.term, dataType: "json", success: function( data ) { response(data); } }); }, minLength: 1 }); }); 添加到您的userLastName输入。

在PHP脚本中添加

id="userLastName"

然后改变

if($_GET['userLastName']) {
    $userLastName = $_GET['userLastName'];
} else $userLastName = null;

BY

SELECT userFirstName FROM user 
WHERE userFirstName LIKE :term