Passing an Array to ajax using $wpdb class

I am having trouble with $wpdb and using an array in a ajax call.

I keep getting a cannot read property ‘0’ of null. So I know I am not creating the array correctly or I am not passing the array to my ajax correctly.

Read More

I know the Select statement works because when I just echo a single value to my ajax call, I get the desired results.

I was hoping a second pair of eyes can see what I am doing wrong. Thanks!

Here is my php Function where I assign values to my array to be passed to my ajax call.

global $wpdb;

if(isset($_POST['post_personid'])) {

    $id = $_POST['post_personid'];

    $stmt = $wpdb->get_results("
                    SELECT a.*, b.*, h.*, d.*, e.*, f.*
                    FROM {$wpdb->prefix}acl_client_info AS a
                    INNER JOIN {$wpdb->prefix}acl_outgoing_calls AS b ON a.id = b.client_id
                    INNER JOIN {$wpdb->prefix}acl_incoming_calls AS h ON a.id = h.client_id
                    INNER JOIN {$wpdb->prefix}acl_services AS d on a.service = d.service_id
                    INNER JOIN (SELECT service_id AS service_type_id, service_name AS service_type_name FROM {$wpdb->prefix}acl_services) AS d ON a.service_type = d.service_type_id
                    INNER JOIN {$wpdb->prefix}acl_find_us AS e ON a.find_us =e.find_us_id
                    INNER JOIN (SELECT find_us_id AS find_id, find_us_name AS find_name FROM {$wpdb->prefix}acl_find_us) AS e ON a.find_us = e.find_id
                    INNER JOIN {$wpdb->prefix}acl_method_of_consulation AS f ON a.method = f.method_id
                    WHERE a.id =". $id ."
                    ", ARRAY_A );

    $viewRetainedClient = array();

    foreach($stmt as $results){

        $viewRetainedClient[] = $results['first_name'];

        /*$viewRetainedClient[] = $results->id; //0 */

        echo json_encode($viewRetainedClient);
    }
}
wp_die();

I have used both ARRAY_A and ARRAY_N with no luck.

Here is my ajax call.

   $(document).on("click", "#personid", function(){

    var personid =$(this).val();

    var data = {
        action: 'myviewclient',
        post_personid: personid
    };

    $.post(
        the_ajax_script.ajaxurl,
        data,
        function(response)
        {
            $("#retained_first_name").val(response[0]);

           /* $("#retained_first_name").html(response[0]); */


        }, "json"

    );

});

I have built this plugin successfully without the $wpdb class and this part worked. Now that I am trying to go back and add $wpdb, its just giving me all kinds of problems. I have read and reread the documentation but I just cant get this to work.

Thanks for your time!

Related posts