SR1092 SR1092 - 2 months ago 52
jQuery Question

Autocomplete Textbox from Active Directory usind LDAP Users in php

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! :)

Answer

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!";
        } }