WP Ajax Function Always Returning undefined

Question

What is the deal here?

JS FUNCTION ‘custom.js’

function validate() {
    var email = jQuery("#billingemail").val();

    if (isValidEmailAddress(email)) {
        jQuery.ajax({
            type: 'POST',
            url: ajaxurl,
            data: {"action": "custome_ajax_email_check", "guestemail": email },
            success: function(data){
                data = jQuery.parseJSON(data);
                if(data.result) {
                    alert('Email Exists');
                    return false;
                } else {
                    alert('Email Does Exists');
                    return true;
                }
            }
        });
    } else {
        return false;
    }
}

JS ON CLICK ‘custom.js’

 $('#mwb_logincoupon').on('click', '#validateguestemail', function (e) {
        e.preventDefault();
        validateemail = validate();
        

PHP AJAX

add_action( 'wp_ajax_custome_ajax_email_check', 'custome_ajax_email_check' );
add_action('wp_ajax_nopriv_custome_ajax_email_check', 'custome_ajax_email_check');

function custome_ajax_email_check(){
    $email = $_POST['guestemail'];

    // do check
    if ( email_exists($email) ) {
        $response->result = true;
    }
    else {
        $response->result = false;
    }

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

debugging the script the JS code goes into the success function, so far as even triggering off the alert('Email Exists').. but the return statement in the functon is never retrieved thus making validateemail always return undefined. Why?

0
, , Brian Bruman 3 years 2020-07-16T00:10:20-05:00 0 Answers 60 views 0

Leave an answer

Browse
Browse