0

First create ajax function to trigger when input field filled

jQuery(document).on('change, keyup', '.cert_number input', function(e) {
 get_key = jQuery(this).val();
 jQuery.ajax({
        url: '<?php echo admin_url('admin-ajax.php'); ?>',
        type: 'post',
        data: { 
         action: 'data_chk_cert_status', 
         bulk_cert_id: get_key,
        },
        success: function(data) {
            console.log('status: '+data);
            if(data==1){
             jQuery('.cert_number input').addClass('error');
             jQuery('#gform_submit_button_10').attr('disabled', true);
             alert('Certificate no exist! Please use different number');
            }else{
             //console.log('good to go!');
             jQuery('.cert_number input').removeClass('error');
             jQuery('#gform_submit_button_10').removeAttr('disabled');
            }
        },
        error: function() {
            console.log("Error");
   }
    });
});

Next Create Ajax function

add_action('wp_ajax_data_chk_cert_status' , 'data_chk_cert_status');
add_action('wp_ajax_nopriv_data_chk_cert_status','data_chk_cert_status');
function data_chk_cert_status(){
 // WP_Query arguments
 $bulk_id = $_POST['bulk_cert_id'];
 $args = array (
     'post_type'              => array( 'bulk_certificate' ),
     'meta_query'             => array(
         array(
             'key'       => 'sellers_certificate_number',
             'value'     => $bulk_id,
         ),
     ),
 );
  // The Query
 $query = new WP_Query( $args );
  // The Loop
 if ( $query->have_posts() ) {
     echo '1';
 } else {
     echo '0';
 }
  // Restore original Post Data
 wp_reset_postdata();
 die();
}

Jagdish Sarma Asked question April 25, 2023
Add a Comment