I'm making an AJAX request in wordpress for the first time and I'm trying to access my custom made table to retrieve single name (string) or multiple names. It all works without the errors, except for one thing, that when the ajax request is done, I'm trying to console.log the value and it just shows Array or Array0 in console. No matter how I change my sql select query, whether it's * or name, it keeps returning Array and that's it. Here is the code:


function my_ajax_handler(){
    global $wpdb;
    $name = $wpdb->get_results("SELECT name FROM username");

        echo $name;

add_action( 'wp_ajax_call_my_ajax_handler', 'my_ajax_handler' );
add_action( 'wp_ajax_nopriv_call_my_ajax_handler', 'my_ajax_handler' );

function enqueue_styles() {
    wp_enqueue_style("child-style", get_template_directory_uri() .'/style.css');
    wp_enqueue_style("parent-style", "https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css");
    wp_enqueue_script("jquery", 'https://code.jquery.com/jquery-3.3.1.min.js"
    wp_enqueue_script( 'my-ajax-script', get_stylesheet_directory_uri() . '/script.js', array('jquery') );
wp_localize_script( 'my-ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
    wp_localize_script( 'my-ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );

add_action("wp_enqueue_scripts", "enqueue_styles");


    url: my_ajax_object.ajax_url,
    data: { 'action': 'call_my_ajax_handler' }
}).done(function(data) {

My database looks like this:

enter image description here

the table is called username

How do I get the name string or multiple strings in console.log in my AJAX function once it's successful? Thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.