I can’t seem to figure out why nothing is returned from my ajax call (returns a 0). What I’m trying to do is when a user fills out their LAN ID on a form, their supervisor’s information auto populates a few fields. Any help/suggestions is much appreciated. Here’s my code:
add_action(‘wp_ajax_nopriv_get_ldapattr’, ‘get_ldap_attr’);
jQuery(function() {
jQuery('#empLanId').on('blur', function() {
var lanid = jQuery('#empLanId').val(),
data = { action: "get_ldap_attr", lanid: lanid };
jQuery.ajax({
url: ajaxurl,
dataType: 'json',
data: data,
success: function(response) {
console.log(response);
},
error: function() {
console.log('error');
}
});
});
});
function get_ldap_attr($lanid) {
$dn = get_site_option ( "ldapServerOU" );
$usr = get_site_option ( "ldapServerCN" );
$pw = get_site_option ( "ldapServerPass" );
$addr = get_site_option ( "ldapServerAddr" );
$ids = array();
$ad = ldap_connect ( $addr )
or die ( "Connection error." );
ldap_set_option ( $ad, LDAP_OPT_PROTOCOL_VERSION, 3 );
ldap_set_option ( $ad, LDAP_OPT_REFERRALS, 0 );
$bind = ldap_bind ( $ad, $usr, $pw );
if ( $bind ) {
$SearchFor ="cn=".$lanid;
$result = ldap_search ( $ad,$dn,$SearchFor );
$entry = ldap_first_entry ( $ad, $result );
if ( $entry != false ) {
$info = ldap_get_attributes ( $ad, $entry );
}
$comm = stripos ( $info['directReports'], ',' );
// find position of first comma in CN=Mxxxxxx,OU=Users,OU=MCR,DC=mfad,DC=mfroot,DC=org (directReports field)
$eq = stripos ( $info['directReports'], '=' );
// find position of first =
$s_lanid = substr ( $info['directReports'], $eq+1, ( ( $comm-1 ) - ( $eq ) ) );
//get substring between = and comma... for lanid happiness..
$sup = getLDAP ( $s_lanid, $ad, $dn, $usr, $pw );
// get supervisor's info...
}
//return $sup;
echo json_encode($sup); die();
If your
$sup
variable is valid and populated after your site option retrieval and parsing, you need to actuallyecho
out theJSON
– not return it.Example in your code:
..should read:
if you write
so you have to call
get_ldapattr
and notget_ldap_att
.In other words, if you specify
you have to call
ACTION_HANDLER
in your ajax js script (unlessACTION_HANDLER
coincides withCALLBACK_FUNCTION
)that’s because it returns results from conditional that checks if user is logged as admin.
0 is false.
Update:
Try instead of return on end of the function, to put echo or print results. I think you end function procedure, but other processes from wp keep getting still.
You’re missing the “header” section.
Your PHP script should end like this: