php - WP User Query & wp-admin/admin-ajax returns 0 with data

I have a odd problem and i'm hoping one of you could guide me to the answer. I am new to PHP and JQuery, so i'm not really sure what is causing this, but essentially, I have a ajax call for a custom search field that works. The "problem" is that after every search result it returns a very random '0' after the results. So for example if I do a search, i'll get something like:

Stack Overflow 0

I've tried seeing what has caused it in the code but I can't iron it out, and as far as I can tell it just...randomly appears there. I don't mind it showing up, if I had a way to apply a css class to it so I could hide it. Anyways, here's my code:


    $args = array(
        'role' => 'artist',
        'order' => 'ASC',
        'orderby' => 'display_name',
        'meta_query' => array(
           
        )
    );

    if (isset($_POST['artistFilter'])){
        $name = explode(" ",$_POST['artistFilter']);

        $args['meta_query'][] = array(
            'key' => 'first_name',
            'value' => $name[0],
            'compare' => 'LIKE'
        );
        $args['meta_query'][] = array(
            'key' => 'last_name',
            'value' => $name[1],
            'compare' => 'LIKE'
        );
    };

    if (isset($_POST['mediumType'])){
        // WP_User_Query arguments
        $args['meta_query'][] = array(
            'meta_key' => 'artist_medium',
           'value'=> esc_attr($_POST['mediumType']),
           'compare' => 'LIKE'
        );
    };


    if (isset($_POST['locationType'])){
        $args['meta_query'][] = array(
            'meta_key'=>'studio_region_location',
            'value' =>$_POST['locationType'],
            'compare' => 'LIKE'
        );
    };

    $user_query = new WP_USER_QUERY ($args);

    if (! empty($user_query->get_results())){
        foreach ($user_query->get_results() as $user){
            $artistimage = get_field('primary_image', 'user_' . $user->ID);
            echo '<div class="artistSearchColumn">';
            if ($artistimage){
                echo '<a href="/staging/author/' . esc_html($user->user_login) . '"><img src="' . esc_html($artistimage['url']) . '" alt="Test"/>';
                echo '<p>' . $user->first_name. ' ' . $user->last_name .'</p></a>';
            }
            else {
                 echo '<a href="/staging/author/' . esc_html($user->user_login) . '">' . esc_attr($user->first_name) . ' ' . esc_attr($user->last_name) . '</a>';
            }
            echo '</div>';
        }
    } else {
        echo '<h1>No Users Found.</h1>';
    }

my jquery:

<script>
jQuery(function($){
    var ajaxscript = {ajax_url: './staging/wp-admin/admin-ajax.php'}
    $('#filter').submit(function(){
        var filter = $('#filter');
        $.ajax({
            url:filter.attr('action'),
            data:filter.serialize(), // form data
            type:filter.attr('method'), // POST
            beforeSend:function(xhr){
                filter.find('button').text('Finding Artists...'); // changing the button label
            },
            success:function(data){
                alert(data);
                filter.find('button').text('Apply filter'); // changing the button label back
                $('#response').html(data); // insert data
            }
        });
        return false;
    });
});
    </script>

Replacing $('#response').html(data) with anything besides data will not return the 0, so I feel like tis related to that.

Thank you for any help!!!

Answer

Solution:

I figured it out, I have to kill the PHP function manually. I simple added

die();

to the end of the script and it doesn't return the 0. Yay! :)

 if (! empty($user_query->get_results())){
        foreach ($user_query->get_results() as $user){
            $artisttour = get_field('artist_tour_status', 'user_'.$user->ID);
            if ($user->artist_profile_display == 'Yes'):
                if ($artisttour != 'Not on Tour'):
                    $artistimage = get_field('primary_image', 'user_' . $user->ID);
                    echo '<div class="artistSearchColumn">';
                    if ($artistimage){
                        echo '<a href="/staging/author/' . esc_html($user->user_login) . '">';
                        echo '<div class="artistBGImage" style="background: url(' . esc_html($artistimage['url']) .');">';
                        echo '<img src="https://monadnockart.org/wp-content/plugins/foo/arttour25-2020icon-2.jpg" alt="'.esc_html($user->user_login).'s Artist Profile"/>';
                        echo '</div>';
                        echo '<p>' . $user->first_name. ' ' . $user->last_name .'</p>';
                        echo '</a>';
                    }
                    else {
                        echo '<a href="/staging/author/' . esc_html($user->user_login) . '"><p>' . esc_attr($user->first_name) . ' ' . esc_attr($user->last_name) . '</p></a>';
                    }
                    echo '</div>';
                else:
                    $artistimage = get_field('primary_image', 'user_' . $user->ID);
                        echo '<div class="artistSearchColumn">';
                        if ($artistimage){
                            echo '<a href="/staging/author/' . esc_html($user->user_login) . '">';
                            echo '<div class="artistBGImage" style="background: url(' . esc_html($artistimage['url']) .');">';
                            
                            echo '</div>';
                            echo '<p>' . $user->first_name. ' ' . $user->last_name .'</p>';
                            echo '</a>';
                        }
                        else {
                            echo '<a href="/staging/author/' . esc_html($user->user_login) . '"><p>' . esc_attr($user->first_name) . ' ' . esc_attr($user->last_name) . '</p></a>';
                        }
                        echo '</div>';
                    
                endif;
            else:
                echo '';
            endif;
            }
        }
    
    else {
    echo '<h2>No Artists Found.</h2>';
        }

        die();
}

Source