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'];
$lc = ldap_connect("***********") or
die("Couldn't connect to AD!");
ldap_set_option($lc, LDAP_OPT_PROTOCOL_VERSION, 3);
$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!";
echo "</body></html>";
//echo $address;
?> <script>
function __change_name() {
var email = $('#select_name option:selected').attr('data-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 :)

Member Avatar

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, learning, and sharing knowledge.