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 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误