0

i have been able to fetch data with an ajax call from active directory .The php file used to fetch data from active directory is below :

<?php
$username   = 'maxxxxxxx';
$password   = 'xxxxxxxxx';
$server = 'ldap://xxxxxxx';
$domain = '@asia.xxxxxx.com';
$port       = 389;

$ldap_connection = ldap_connect($server, $port);

if (! $ldap_connection)
{
    echo '<p>LDAP SERVER CONNECTION FAILED</p>';
    exit;
}

// Help talking to AD
ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0);

$ldap_bind = @ldap_bind($ldap_connection, $username.$domain, $password);

if (! $ldap_bind)
{
    echo '<p>LDAP BINDING FAILED</p>';
    exit;
}
else
{
  echo 'login successful';
}

$base_dn = "OU=Employees,OU=Accounts,OU=India,DC=asia,DC=xxxxx,DC=com";

$dispname="Mark Hwett";


$filter ="(&(objectClass=user)(displayName=$dispname))";

$attr = array("sn","givenname","employeeid","distinguishedname","displayname","samaccountName","department","manager","mail","title","thumbnailphoto");

$result = ldap_search($ldap_connection,$base_dn,$filter,$attr);

$rescount = ldap_count_entries($ldap_connection,$result);

$data = ldap_get_entries($ldap_connection,$result);


if ($data["count"] > 0)
{
for ($i=0; $i<$data["count"]; $i++)
{
echo "<p> sn: " . $data[$i]["sn"][0]."<br/>";
echo "givenname: ". $data[$i]["givenname"][0] ."<br/>" ;
echo "employeeID: " . $data[$i]["employeeid"][0]."<br/>";
echo "distinguishedName: " . $data[$i]["distinguishedname"][0]."<br/>";
echo "displayName: " . $data[$i]["displayname"][0]."<br/>";
echo "sAMAccountName: " . $data[$i]["samaccountname"][0]."<br/>";
echo "department: ". $data[$i]["department"][0]."<br/>";
echo "manager: " .$data[$i]["manager"][0]."<br/>";
echo "mail: ". $data[$i]["mail"][0]."<br/>";
echo "title: " .$data[$i]["title"][0]."<br/>";
//echo "photo: " .$data[$i]["thumbnailphoto"][0]."<br/>";


// echo "<br/><br/>";
}
}
else
        {
            echo "<p>No results found!</p>";
        }


?>

The browser console shows that the above php returns this :

<p> sn: xxxxxx<br/>givenname: xxxxx<br/>
employeeID: 0050<br/
>distinguishedName: CN=xxxx xxxxx,OU=Employees,OU=Accounts,OU=India,DC=asia,DC=xxxxxxx,DC=com<br/>
displayName: Mark Hewettk<br/>sAMAccountName: xxxxxxx<br/>
department: xxxxx<br/>manager: CN=xxxxxx xxxxxxx,OU=Employees,OU=Accounts,OU=India,DC=asia,DC=xxxx,DC=com
<br/>
mail: mhewettk@abc.com<br/>
title: xyz<br/>

I want to take only some attributes from above data like mail,displayname etc and display in my HTML :

  <h2 class="profile__name" id="emailOfUser">Email : </h2>

Now the problem is the jquery that I have used here :

$('.leaderboard li').on('click', function () {
 $.ajax({
 url: "../popupData/activedirectory.php", // your script above a little adjusted
 type: "POST",
 data: {id:$(this).find('.parent-div').data('name')},
 success: function(data){
 console.log(data);

 $('#popup').fadeIn();
 
$('#emailOfUser').html(data); //this line displays all data whereas I want to select only email,displayname from the above console data



//whatever you want to fetch ......
 // etc ..
 },
 error: function(){
 alert('failed, possible script does not exist');
 }
 });
 });

problem is this :

$('#emailOfUser').html(data); 

this line displays all data whereas I want to select only email,displayname from the above console data

kindly help me how to select only desired attribute data from the above browser console data.

I eve tried to convert it to JSON following this.but no luck.How to return an array from an AJAX call?

Community
  • 1
  • 1
JAne
  • 97
  • 1
  • 10

1 Answers1

0

Instead of passing the data to the client as a string of HTML you could pass the data as a simple JSON-Object:

header('Content-Type: application/json');
echo json_encode($data);

That way you can process the data much easier on the client side as it is already in a format that JavaScript understands.

Something like...

$.ajax({
   url: "../popupData/activedirectory.php"
   ...
}).then(function(response) {
   // now you have access to the data as a plain JS-Object
   $('#emailOfUser').html(response.email + " - " + response.displayname);
});

... should do the trick.

Đinh Carabus
  • 3,403
  • 4
  • 22
  • 44
  • thanks for the response.but I am getting errors "saying possible script doesnot exist". I ahve mag ethe following changes to the .php file above http://pastebin.com/fBDJqKFQ AND the following changes to the jquery http://pastebin.com/JTHeAjsi – JAne Jan 30 '17 at 19:45
  • Where do u think is the mistake ? – JAne Jan 30 '17 at 19:45
  • can you post a link to the output of console.log? – Đinh Carabus Jan 30 '17 at 19:53
  • its in my local drive, otherwise would have shared the link.the console displays the data as mentioned above in the second snippet.i have copied and pasted it from console only. – JAne Jan 30 '17 at 19:56
  • Can you check in the network-tab of the browser devtools what the response from the server looks like? Do you even get a response? Did you change any parameters of the $.ajax-call? – Đinh Carabus Jan 30 '17 at 20:23
  • its all okay.i have checked otherwise i wiuldnot get the data in the browser console. – JAne Jan 30 '17 at 20:25
  • are files in the pastebinn link okay ? – JAne Jan 30 '17 at 20:26
  • If jQuery only executes the error-callback for your ajax-call ("saying possible script doesnot exist"), there must be something wrong with the server-response. Sorry if I'm not following you ...the pastebin links you posted only contain the code, not the response-data itself(?). – Đinh Carabus Jan 30 '17 at 20:33