doufu4333 2015-08-25 06:39
浏览 65
已采纳

使用php中的LDAP用户从Active Directory自动完成文本框

I am trying to autocomplete a field that picks names of users from LDAP. My codes are as follows :

index.php

$(document).ready(function() {
  $("#search-box").keyup(function() {
    $.ajax({
      type: "POST",
      url: "readCountry.php",
      data: 'keyword=' + $(this).val(),
      beforeSend: function() {
        $("#search-box").css("background", "#FFF url(LoaderIcon.gif) no-repeat 165px");
      },
      success: function(data) {
        $("#suggesstion-box").show();
        $("#suggesstion-box").html(data);
        $("#search-box").css("background", "#FFF");
      }
    });
  });
});

function selectCountry(val) {
  $("#search-box").val(val);
  $("#suggesstion-box").hide();
}
body {
  width: 610px;
}
.frmSearch {
  border: 1px solid #F0F0F0;
  background-color: #C8EEFD;
  margin: 2px 0px;
  padding: 40px;
}
#country-list {
  float: left;
  list-style: none;
  margin: 0;
  padding: 0;
  width: 190px;
}
#country-list li {
  padding: 10px;
  background: #FAFAFA;
  border-bottom: #F0F0F0 1px solid;
}
#country-list li:hover {
  background: #F0F0F0;
}
#search-box {
  padding: 10px;
  border: #F0F0F0 1px solid;
}
<html>

<head>
  <TITLE>jQuery AJAX Autocomplete - Country Example</TITLE>

  <head>

    <body>
      <div class="frmSearch">
        <input type="text" id="search-box" placeholder="Country Name" />
        <div id="suggesstion-box"></div>
      </div>
    </body>

</html>

readCountry.php

                <?php

            if(!empty($_POST["keyword"])) {

            $Name=$_POST["keyword"];
            $username="********";
            $password="********";
            $lc = ldap_connect("********") or
            die("Couldn't connect to AD!");
            ldap_set_option($lc, LDAP_OPT_PROTOCOL_VERSION, 3);
            ldap_bind($lc,$username,$password);
            $base = "OU=********,DC=********,DC=********";
            $filt = "(&(&(&(objectCategory=person)(objectClass=user)(name=$Name*))))";
            $sr = @ldap_search($lc, $base, $filt);
            $info = ldap_get_entries($lc, $sr);
            for ($i = 0; $i < $info["count"]; $i++) {
            $info[$i]["cn"][0] ;
            }
            if ($i == 0) {
            echo "No matches found!";
            }

            if(!empty($info[$i]["cn"][0])) {
            ?>
            <ul id="country-list">
            <?php
            foreach($info[$i]["cn"][0] as $country) {
            ?>
            <li onClick="selectCountry('<?php echo $country ?>');"><?php echo   $country ?></li>
            <?php } ?>
            </ul>
            <?php } } ?>

What I have :

This isn't returning names from LDAP nor is there any error that is dispalyed for me to fix it.

What I need :

enter image description here

When A is typed, all the names starting with 'A' should be shown in the dropdown.

Appreciate any help :) Thanks in advance! :)

</div>
  • 写回答

2条回答 默认 最新

  • dongmeirang4679 2015-08-25 09:21
    关注

    I found a way out! Just had to change the onclick in readcountry.php as shown below :

                    <?php
    
            if(!empty($_POST["keyword"])) {
    
            $Name=$_POST["keyword"];
            $username="********";
            $password="********";
            $lc = ldap_connect("********") or
            die("Couldn't connect to AD!");
            ldap_set_option($lc, LDAP_OPT_PROTOCOL_VERSION, 3);
            ldap_bind($lc,$username,$password);
            $base = "OU=********,DC=********,DC=********";
            $filt = "(&(&(&(objectCategory=person)(objectClass=user)(name=$Name*))))";
            $sr = @ldap_search($lc, $base, $filt);
            $info = ldap_get_entries($lc, $sr);
            for ($i = 0; $i < $info["count"]; $i++) {
           <li onClick="selectCountry('<?php echo $info[$i]["cn"][0] ?>');"><?php echo $info[$i]["cn"][0] ?></li>
            }
            if ($i == 0) {
            echo "No matches found!";
            } }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果