title: Simple LDAP Search with PHP JQuery
body: <p>I want to do a simple ajax search of LDAP (or AD) using PHP and JQuery. I have read a number of tutorials that search mysql, such as:</p>
  
  <p><a href="http://www.codeforest.net/simple-search-with-php-jquery-and-mysql" rel="nofollow">http://www.codeforest.net/simple-search-with-php-jquery-and-mysql</a></p>
  
  <p>I want to replace the mysql connection with my ldap connection details details below. Could someone help me out in getting this to work as I cant get the results to display.</p>
  
  <p>Thanks</p>
  
  <p>LDAP Connection script</p>
  
  <pre><code>    &lt;?php echo "&lt;?xml version='1.0' encoding='utf-8'  ?&gt;" ?&gt;&lt;?php echo "&lt;ul class='LSRes'&gt;" ?&gt;
  &lt;?php
  if( isset($_GET['q']) &amp;&amp;!empty($_GET['q']) ){
  // all your ldap code
  
  // Designate a few variables
  $host = "10.10.10.10"; // Add in your AD host name or IP
  $user = "DOMAIN\user"; // Add in your AD access account user name
  $pswd = "password"; // Add in your AD access account user name password
  
  $ad = ldap_connect($host)
        or die( "Could not connect!" );
  
  // Set version number
  ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3)
       or die ("Could not set ldap protocol");
  
  // Binding to ldap server
  $bd = ldap_bind($ad, $user, $pswd)
        or die ("Could not bind");
  
  // Create the DN - Add in the OU of your AD
  $dn = "OU=accounts,DC=domain,DC=com";
  
  // Specify only those parameters we're interested in displaying from AD
  $attrs = array("displayname","mail","samaccountname","telephonenumber","givenname", "title");
  //$attrs = array("samaccountname");
  
  // Create the filter from the search parameters
  //$filter = "(|(givenName=".$_GET['q']."*) (sn=".$_GET['q']."*) (displayname=".$_GET['q']."*) (samaccountname=".$_GET['q']."*) (telephonenumber=".$_GET['q']."*))";
  $filter = "(|(givenName=".$_GET['q']."*) (sn=".$_GET['q']."*) (displayname=".$_GET['q']."*) (samaccountname=".$_GET['q']."*))";
  
  $search = ldap_search($ad, $dn, $filter, $attrs)
            or die ("&lt;span class='LSstyle_noresults'&gt;&lt;strong&gt;Could not connect to AD&lt;/strong&gt;&lt;/span&gt;");
  
  // Sort entries by last name ('sn')
  ldap_sort ( $ad, $search, 'sn' ) ;
  //ldap_sort ( $ad, $search, $samaccountname ) ;
  
  $entries = ldap_get_entries($ad, $search);
  
  if ($entries["count"] &gt; 0) {
  for ($i=0; $i&lt;$entries["count"]; $i++) {
  
  
  echo "&lt;span class='LSstyle'&gt;Name: &lt;strong&gt;&lt;a href=\"mailto:".$entries[$i]["mail"][0]."\"&gt;".$entries[$i]["displayname"][0]." ".$entries[$i]["sn"][0]."&lt;/a&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;";
  echo "&lt;span class='LSstyle'&gt;Short name: &lt;strong&gt;".$entries[$i]["samaccountname"][0]."&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;";
  echo "&lt;span class='LSstyle'&gt;Phone: &lt;strong&gt;".$entries[$i]["telephonenumber"][0]."&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;";
  echo "&lt;span class='LSstyle'&gt;Title: &lt;strong&gt;".$entries[$i]["title"][0]."&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;";
  echo "&lt;span class='LSstyle'&gt;Dept: &lt;strong&gt;".$entries[$i]["department"][0]."&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;";
  }
  } else {
  echo "&lt;span class='LSstyle_noresults'&gt;&lt;strong&gt;No results found&lt;/strong&gt;&lt;/span&gt;";
  }
  ldap_unbind($ad);
  } 
  ?&gt;
  </code></pre>