jQuery Validator Plugin - check for existing Username/Email in mysql database

后端 未结 3 1522
天涯浪人
天涯浪人 2020-12-16 01:49

I\'ve successfully created a form that submits and adds users to a mysql database, and form validation with \'jQuery Validator\' plugin works great for everything except che

3条回答
  •  鱼传尺愫
    2020-12-16 02:15

    For WordPress & PHP

    Most important thing is instead or return true or false, need to echo 'true' or 'false'

    jQuery or JS

       email: {
         required: true,
         minlength: 6,
         email: true,
         remote:{
            url : 'your ajax url',
            data: { 
                    'action': 'is_user_exist',
                  },
            },
       },
    

    WordPress or PHP Backend Code

    In backend you will automatically get the value of field via GET method.

    /*
     *@ Check user exists or not
     */
    if( !function_exists('is_user_exists_ajax_function') ):
        function is_user_exists_ajax_function() {
            $email = $_GET['email'];
            if( empty($email) && is_email($email) ):
                wp_send_json_error( new \WP_Error( 'Bad Request' ) );
            endif;
    
            $is_email = email_exists( $email );
    
            if($is_email):
                echo 'false';
            else:
                echo 'true';
            endif;
    
            wp_die();
        }
        add_action( 'wp_ajax_is_user_exist', 'is_user_exists_ajax_function' ); 
        add_action( 'wp_ajax_nopriv_is_user_exist', 'is_user_exists_ajax_function' ); 
    endif;
    

提交回复
热议问题