I have a form that need user verifaction from ldap before being submitted. I have an ajax code for the same, to get the value of the field onchange and query ldap so that it returns the name and email id. My code is as below :

<input type="text" name="Admin_name" class="form-control" id="Admin_name" ><br/> <?php
$Name = $_POST['Supervisor_Name'];
$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=IMBANK-KENYA,DC=inm,DC=corp";
$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++) {
  //echo  $address=$info[$i]["cn"][0]."<br/>" ;
  //echo  $address=$info[$i]["mail"][0]."<br/>" ;
  //echo  $address=$info[$i]["title"][0]."<br/><br/>";
  //echo  $address=$info[$i]["id"][0]."<br/>";
}
if ($i == 0) {
  echo "No matches found!";
}
ldap_close($lc);
echo "</body></html>";
//echo $address;
?> <script>
function __change_name() {
var email = $('#select_name option:selected').attr('data-email');
$('#select_email').val(email);
}
</script> <select name= "name" id="select_name" onchange="__change_name()"> <?php
for ($i = 0; $i < $info['count']; $i++) {
echo '<option value="' . $info[$i]['sn'][0] . ', ' . $info[$i]['givenname'][0] . '" data-email="' . $info[$i]['mail'][0] . '">' . $info[$i]['sn'][0] . ', ' . $info[$i]['givenname'][0] . '</option>';
}
?> </select> <input type="text" name="email" id="select_email">

But this doesn't seem to work. Where am I going wrong? Appreciate any help :)

Dani AI

Generated

Immediate things to fix (concrete)

  • The form input name/id your page renders does not match the POST key your PHP reads, so the LDAP filter is empty. Fix the client-side name/id or read the correct $_POST key on the server.
  • The page mixes full HTML output and an AJAX response (you echo closing </body></html>), which breaks a clean client/server contract. Return structured data from the server and let the client build UI markup.
  • Use a dedicated LDAP endpoint as suggested and do not put bind credentials or LDAP logic in client-visible files.

What the server endpoint should do (secure, minimal)

  • Accept a single parameter and validate it. Escape it for filter use with PHP ldap_escape (ldap_escape) to prevent LDAP injection.
  • Bind over LDAPS or StartTLS and use a service account whose credentials are stored outside webroot or in environment variables. See ldap_start_tls for StartTLS notes.
  • Perform a bounded search: restrict base/scope, request only needed attributes (cn, mail), and set size/time limits. Return JSON using json_encode and set header Content-Type: application/json (json_encode).

Client-side best practices and testing

  • Debounce input (≈300ms) and require a minimum length (2–3 chars) before querying.
  • Request JSON (dataType: 'json') and populate the select from the returned array.
  • Use the browser Network tab to verify request method, parameter names, payload, response code and JSON shape. Enable PHP error reporting and check ldap_error() if searches fail.

Example response shape (server returns this)

[{"name":"Doe, John","email":"john.doe@example.com"}]

Quick checklist to get working

  1. Make names/POST keys match. 2) Create ldap_check.php that validates/escapes input and returns JSON. 3) Call it from the page with a debounced AJAX request. 4) Inspect Network tab and LDAP error text if results are unexpected.
Member Avatar for Member #120589

I can't make much sense of this. Is all of this in one page?

I can't see how this could work. It would be useful if you described what your script was supposed to do.

However, lacking any further info, I'd probably do this:

ldap_check.php - contains ldap connection and check. If ajax request - echo output, if not, return output

page.php - page with the form and the js (jQuery reference and own script)

You have a choice with regard to the output from the call: either get raw data from the ldap and pass it to js e.g. as json and let js build the markup OR get PHP to build the entire output including markup and get js to just show it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.