AJAX - Returning Two JSON Objects with One PHP Function

by shapada   Last Updated July 26, 2018 09:08 AM

How would I go about returning two JSON objects with one AJAX call and PHP function? Any help is much appreciated!

Here's the PHP function:

function get_ldap_attr() {
    header("Content-type: application/json");
    $lan = $_POST['lan'];
    $dn = get_site_option ( "ldapServerOU" );
    $usr = get_site_option ( "ldapServerCN" );
    $pw = get_site_option ( "ldapServerPass" );
    $addr = get_site_option ( "ldapServerAddr" );
    $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=".$lan;
            $result = ldap_search ( $ad,$dn,$SearchFor );
            $entry = ldap_first_entry ( $ad, $result );
            if ( $entry != false )  {
                $info = ldap_get_attributes ( $ad, $entry );
            }           
            $comm  = stripos ( $info['manager'][0], ',' );
                // find position of first comma in CN=Mxxxxxx,OU=Users,OU=MCR,DC=mfad,DC=mfroot,DC=org  (directReports field)
            $eq = stripos ( $info['manager'][0], '=' );
                // find position of first =
            $s_lanid = substr ( $info['manager'][0], $eq+1, ( ( $comm-1 ) - ( $eq ) ) );
                //get substring between = and comma... for lanid happiness..
            $sup = getLDAPInfo ( $s_lanid, $bind, $ad, $dn ); 
                // get supervisor's info...
    }

    echo json_encode($sup);
    die();  
}

And the jQuery:

jQuery(function() {
    jQuery('#empLanId').on('blur', function() {
        var lan = jQuery('#empLanId').val();
        var ajaxurl = '<?php echo admin_url("admin-ajax.php", null); ?>';
        var data = { action: "get_ldap", lan: lan};
        jQuery.ajax({
            type: 'POST',
            url: ajaxurl,
            data: data,
            dataType: 'json',
            success: function(response) {
                jQuery('#empSupLanId').val(response.lanid);
                jQuery('#empSupName').val(response.fullname);
                jQuery('#empSupNumber').val(response.phone);
            }
        });
    });
});
Tags : php jquery ajax


Answers 2


If I get it correctly, You need to wrap them in an array and pass to Javascript.

function get_ldap_attr() {
    header("Content-type: application/json");
    ...

    // suppose you want to return $second_var
    echo json_encode(array($sup,$second_var));
    die();  
}

Success function of JSON request will receive them as array. You can access them with integer index.

jQuery(function() {
    jQuery('#empLanId').on('blur', function() {
        var lan = jQuery('#empLanId').val();
        var ajaxurl = '<?php echo admin_url("admin-ajax.php", null); ?>';
        var data = { action: "get_ldap", lan: lan};
        jQuery.ajax({
            type: 'POST',
            url: ajaxurl,
            data: data,
            dataType: 'json',
            success: function(response) {
                response[0] // first $sup variable
                response[1] // second $second_var variable
                //jQuery('#empSupLanId').val(response.lanid);
                //jQuery('#empSupName').val(response.fullname);
                //jQuery('#empSupNumber').val(response.phone);
            }
        });
    });
});
M-R
M-R
July 26, 2013 17:56 PM

<script type="text/javascript">
  $(document).ready(function(){

    //jquery script
    $("#emp_mat").change(function(){

      var emp_mat = $(this).val();
    $.ajax({
        url:"emp_info.php",
        dataType:'json',
        data:{data:emp_mat}
      }).done(function(result)
      {
        $("#nom_prenom").val(result[0]);
        $("#adresse").val(result[1]);

      })
    });

  });
</script>

<input type="text" id="nom_prenom"   disabled="true" size="58"/>
<input type="text" id="adresse"   disabled="true" size="58"/>
Ahdi Kaddes
Ahdi Kaddes
July 26, 2018 08:57 AM

Related Questions




Jquery not appearing on homepage

Updated January 22, 2018 07:08 AM