0

I am having a bit of a weird glitch happening to me and I am not sure how I have produced it or whether it is the norm.

I am developing my own plugin and when a football/soccer team is typed into a box it will check if its in the database already or not.

Here are my lines of code

add_action( 'admin_footer', 'fws_teamcheck_javascript' );
function fws_teamcheck_javascript() { ?>
<script type="text/javascript">
    jQuery(document).on('focusout', '.name', function () {
        var name = this.value;
        //alert(name);
        jQuery.ajax({
            type: 'POST',
            url: ajaxurl,
            data: {"action": "fws_check_name", "name": name},
            success: function (data) {
                alert(data);
                if(data=='true') {
                    $(".name").css("border", "solid 1px red");
                }
                if(data=='false') {
                    $(".name").css("border", "solid 1px green");
                }
            }
        });
    });
</script>
<?
}

function fws_check_name(){
    global $wpdb;
    $dbtable = $wpdb->prefix . 'fws_team_data';
    $name = $_POST['name'];
    $data = $wpdb->get_results($wpdb->prepare("SELECT * FROM $dbtable WHERE name = $name"));
    if($data) { echo 'true'; }else{ echo 'false'; }
    
}
add_action( 'wp_ajax_fws_check_name', 'fws_check_name' );
add_action( 'wp_ajax_nopriv_fws_check_name', 'fws_check_name' );

When i move away from the input it will bring up the alert as either true0 or false0 is there a reason why it is adding the 0 at the end?

Thanks for your input.

2
  • have you considered using the modern REST API for AJAX instead of the old admin-ajax.php API? It's much easier to debug, gives human readable error messages and does a lot of the heavy lifting for you, as well as giving you a pretty URL.
    – Tom J Nowell
    Commented Nov 3, 2021 at 17:11
  • @TomJNowell to be honest I haven't even looked at REST API, I am still trying to learn the basics from online tutorials. Do you have any recommendations on where I can learn REST API for wordpress?
    – AdamCarder
    Commented Nov 3, 2021 at 17:19

2 Answers 2

0

The following wasn't wrong for me:

$data = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $dbtable WHERE name = $name" ) );

It should be:

$data = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$dbtable} WHERE name = %s", $name ) );

Then you can just die(); at the end of the function like:

if( count( $data ) >= 1 ){
    die( 'true' );
} else {
    die( 'false' );
}
0

I found out when re-reading through the AJAX page found at https://codex.wordpress.org/AJAX_in_Plugins that you have to put die(); at the end of the function.

My code now looks like this and works as intended.

add_action( 'admin_footer', 'fws_teamcheck_javascript' );
function fws_teamcheck_javascript() { ?>
<script type="text/javascript">
    jQuery(document).on('focusout', '.name', function () {
       var name = this.value;
       jQuery.ajax({
           type: 'POST',
           url: ajaxurl,
           data: {"action": "fws_check_name", "name": name},
           success: function (data) {
               if(data=='true') {
                   $(".name").css("border", "solid 1px red");
               }
               if(data=='false') {
                   $(".name").css("border", "solid 1px green");
               }
           }
       });
    });
</script>
<?
}

function fws_check_name(){
    global $wpdb;
    $dbtable = $wpdb->prefix . 'fws_team_data';
    $name = $_POST['name'];
    $sql = $wpdb->prepare("SELECT * FROM $dbtable WHERE name = '$name'");
    $data = $wpdb->get_results($sql);
    if(count($data) >= '1'){ echo 'true'; }else{ echo 'false'; }
    die();
    
}
add_action( 'wp_ajax_fws_check_name', 'fws_check_name' );
add_action( 'wp_ajax_nopriv_fws_check_name', 'fws_check_name' );
1
  • You might find wp_send_json useful for your project as well.
    – admcfajn
    Commented Nov 3, 2021 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.