- Home
- Categorie
- Coding e Sistemistica
- PHP
- Ricerca autocomplete jquey su Active Directory Windows
-
Ricerca autocomplete jquey su Active Directory Windows
Buongiorno a tutti
premesso che non sono un esperto, sto cercando di fare una ricerca per user o nome su AD.
Ho trovato degli esempi e ho implementato due pagine:- test_ad.php
- leggi.php
Non riesco a passare variabili (credo) e la ricerca rilascia tutti i nominativi ma non ricerca per nome.
Potete aiutarmi? Grazie in anticipo.test_ad.php
[PHP]
<!DOCTYPE html>
<html>
<head>
<title>Autocomplete textbox using jQuery, PHP and LDAP</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"; />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>;
<style>
ul{
background-color:#eee;
cursor:pointer;
}
li{
padding:12px;
}
</style>
</head>
<body>
<br /><br />
<div class="container" style="width:500px;" action="test_ad.php" method="POST">
<h3 align="center">Autocomplete textbox using jQuery, PHP and LDAP</h3><br />
<label>Enter Name</label>
<input type="text" name="search-box" id="search-box" class="form-control" placeholder="Enter Name" />
<div id="suggesstion-box"></div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
$('#search-box').keyup(function(){
//var query = $('#search-box').val();
var query = $('#search-box').val();
if(query != '')
{
$.ajax({
url:"leggi.php",
method:"REQUEST",
data:{query:query},
success:function(data)
{
$('#suggesstion-box').fadeIn();
$('#suggesstion-box').html(data);
}
});
}
});
$(document).on('click', 'li', function(){
$('#search-box').val($(this).text());
$('#suggesstion-box').fadeOut();
minLength: 3
});
});
</script>
[/PHP]leggi.php
[PHP]
<!DOCTYPE html>
<html>
<head>
<title>Autocomplete textbox using jQuery, PHP and LDAP</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"; />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>;
<style>
ul{
background-color:#eee;
cursor:pointer;
}
li{
padding:12px;
}
</style>
</head>
<body>
<br /><br />
<div class="container" style="width:500px;" action="test_ad.php" method="POST">
<h3 align="center">Autocomplete textbox using jQuery, PHP and LDAP</h3><br />
<label>Enter Name</label>
<input type="text" name="search-box" id="search-box" class="form-control" placeholder="Enter Name" />
<div id="suggesstion-box"></div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
$('#search-box').keyup(function(){
//var query = $('#search-box').val();
var query = $('#search-box').val();
if(query != '')
{
$.ajax({
url:"leggi.php",
method:"REQUEST",
data:{query:query},
success:function(data)
{
$('#suggesstion-box').fadeIn();
$('#suggesstion-box').html(data);
}
});
}
});
$(document).on('click', 'li', function(){
$('#search-box').val($(this).text());
$('#suggesstion-box').fadeOut();
minLength: 3
});
});
</script>
[/PHP]
-
Ho sbagliato a inserire il codice di leggi.php! come posso correggere il post?
grazie[PHP]
<?php
set_time_limit(30);
error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
ini_set('display_errors',1);// config
$ldapserver = 'sever';
$ldapuser = 'user';
$ldappass = 'password';
$ldaptree = "OU=user,DC=pippo,DC=it";// connect
$ldapconn = ldap_connect($ldapserver) or die("Could not connect to LDAP server.");if($ldapconn) {
// binding to ldap server
$ldapbind = ldap_bind($ldapconn, $ldapuser, $ldappass) or die ("Error trying to bind: ".ldap_error($ldapconn));
// verify binding
if ($ldapbind) {
echo "LDAP bind successful...<br /><br />";$search_filter = '(&(objectCategory=person)(samaccountname=*))'; $attributes = array(); $attributes[] = 'cn'; $attributes[] = 'mail'; $attributes[] = 'samaccountname'; $attributes[] = 'sn'; $result = ldap_search($ldapconn,$ldaptree,$search_filter) or die ("Error in search query: ".ldap_error($ldapconn)); ldap_sort($ldapconn,$result,"cn"); $data = ldap_get_entries($ldapconn, $result); // iterate over array and print data for each entry echo '<h1>Utenti</h1>'; for ($i=0; $i<$data["count"]; $i++) { //echo "dn is: ". $data*["dn"] ."<br />"; echo "User: ". $data*["cn"][0] ."<br />"; if(isset($data*["mail"][0])) { echo "Email: ". $data*["mail"][0] ."<br /><br />"; } else { echo "Email: None<br /><br />"; } } // print number of entries found echo "Number of entries found: " . ldap_count_entries($ldapconn, $result); } else { echo "LDAP bind failed..."; }
}
// all done? clean up
ldap_close($ldapconn);
?>
[/PHP]
-
[h=2]errata corrige codice esatto La pagina leggi.php funziona se nella url passo la variabile, come posso inviare la variabile dalla pagina con il form di ricerca (test_ad.php)? Non riesco a capire.
grazie![image](https://forum.html.it/forum/images/misc/quote_icon.png) Originariamente inviata da **Linnox** [![image](https://forum.html.it/forum/images/buttons/viewpost-right.png)](https://forum.html.it/forum/showthread.php?p=25545792#post25545792) Buongiorno a tutti
premesso che non sono un esperto, sto cercando di fare una ricerca per user o nome su AD.
Ho trovato degli esempi e ho implementato due pagine:- test_ad.php
- leggi.php
Non riesco a passare variabili (credo) e la ricerca rilascia tutti i nominativi ma non ricerca per nome.
Potete aiutarmi? Grazie in anticipo.test_ad.php
Codice PHP:
<!DOCTYPE html>
<html>
<head>
<title>Autocomplete textbox using jQuery, PHP and LDAP</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"; />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>;
<style>
ul{
background-color:#eee;
cursor:pointer;
}
li{
padding:12px;
}
</style>
</head>
<body>
<br /><br />
<div class="container" style="width:500px;" action="test_ad.php" method="POST">
<h3 align="center">Autocomplete textbox using jQuery, PHP and LDAP</h3><br />
<label>Enter Name</label>
<input type="text" name="search-box" id="search-box" class="form-control" placeholder="Enter Name" />
<div id="suggesstion-box"></div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
$('#search-box').keyup(function(){
//var query = $('#search-box').val();
var query = $('#search-box').val();
if(query != '')
{
$.ajax({
url:"leggi.php",
method:"REQUEST",
data:{query:query},
success:function(data)
{
$('#suggesstion-box').fadeIn();
$('#suggesstion-box').html(data);
}
});
}
});
$(document).on('click', 'li', function(){
$('#search-box').val($(this).text());
$('#suggesstion-box').fadeOut();
minLength: 3
});
});
</script>leggi.php
Codice PHP: set_time_limit(30);
error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
ini_set('display_errors',1);
$key = $_REQUEST['search-box'];
///echo "".$key."<br>";
// config
$ldapserver = 'local';
$ldapuser = 'use';
$ldappass = 'password';
$ldaptree = "OU=xx,DC=xxx,DC=xxx";// connect
$ldapconn = ldap_connect($ldapserver) or die("Could not connect to LDAP server.");
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
if($ldapconn) {// binding to ldap server $ldapbind = ldap_bind($ldapconn, $ldapuser, $ldappass) or die ("Error trying to bind: ".ldap_error($ldapconn)); // verify binding if ($ldapbind) { echo "LDAP bind successful...<br /><br />"; $search_filter = '(&(objectCategory=person)(objectClass=user)(sn=*'. $key .'*))'; $attributes = array(); $attributes[] = 'cn'; $attributes[] = 'mail'; $attributes[] = 'samaccountname'; $attributes[] = 'sn'; $result = ldap_search($ldapconn,$ldaptree,$search_filter,$attributes) or die ("Error in search query: ".ldap_error($ldapconn)); ldap_sort($ldapconn,$result,"sn"); $data = ldap_get_entries($ldapconn, $result); // iterate over array and print data for each entry echo '<h1>Utenti</h1>'; for ($i=0; $i<$data["count"]; $i++) { //echo "dn is: ". $data*["dn"] ."<br />"; echo "User: ". $data*["cn"][0] ."<br />"; if(isset($data*["mail"][0])) { echo "Email: ". $data*["mail"][0] ."<br /><br />"; } else { echo "Email: None<br /><br />"; } } echo "".$key."<br>"; // print number of entries found echo "Number of entries found: " . ldap_count_entries($ldapconn, $result); } else { echo "LDAP bind failed..."; }
}
// all done? clean up
ldap_close($ldapconn);
-
Ciao,
Nella chiamata tramite ajax utilizzi
method:REQUEST
non esiste nessun metodo HTTP che si chiami request.
Devi utilizzare GET oppure POST e lato PHP recuperi il dato con $_GET o $_POST.
Nella chiamata ajax non hai nemmeno specificato il formato dei dati, ti consiglio di utilizzare JSON. Dai un occhio alla doc https://api.jquery.com/jQuery.ajax/
-
Grazie!
stasera provo.