拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 在HTML表单提交上通过Ajax检查Wordpress中是否存在电子邮件

在HTML表单提交上通过Ajax检查Wordpress中是否存在电子邮件

白鹭 - 2022-01-24 1980 0 0

在我的 WordPress v5.8.2 中,我ajax_url在functions.php 中本地化了

wp_enqueue_script('site_scripts', get_stylesheet_directory_uri() . '/assets/js/site-scripts.js', array('jquery'), null, true);

wp_localize_script('site_scripts', 'site_ajax', array('ajax_url' => admin_url('admin-ajax.php'), 'check_nonce' => wp_create_nonce('site_ajax_nonce')));

使用以下 jQuery 脚本,我正在处理表单以检查来自 HTML 表单的电子邮件 ID 是否已存在于 WordPress 中:

   $(document).on("submit", "#form", function(e) {
     e.preventDefault();
     $email = $(this).find('input[name=email]').val(); // email
     //ajax request, check if user exists
     $.ajax({
       type: "GET",
       dataType: 'json',
       url: site_ajax.ajax_url,
       data: {
         email: $email,
         action: 'email_exists_check',
         security: site_ajax.site_ajax_nonce
       },
       success: function(data) {
         if (data.result) {
           alert('Email exists!');
         } else {
           alert('Email does not exists!');
         }
       }
     });
   });

在单独档案中的 PHP 代码下方以检查电子邮件:

add_action('wp_ajax_email_exists_check', 'email_exists_check');
add_action('wp_ajax_nopriv_email_exists_check', 'email_exists_check');

function email_exists_check() {

    // Check nonce and email is set by ajax post.
    if (isset($_POST['email']) && wp_verify_nonce('check_nonce', 'security')) {
        // Sanitize your input.
        $email = sanitize_text_field(wp_unslash($_POST['email']));
        // do check.
        if (email_exists($email)) {
            $response = true;
        } else {
            $response = false;
        }
        // send json and exit.
        wp_send_json($response);
    }
}

如果电子邮件存在,则上述整个代码无法发出警报。

我怎样才能使这段代码作业?

更新 #1

根据@Howard E 的建议,我发现email_exists_check()未加载包含该函式的 PHP 档案

现在加载了 PHP 档案,我没有得到实际email_exists状态。对于存在和不存在的电子邮件,警报始终是电子邮件不存在 ( data.result == false)。

似乎email_exists_check函式本身没有加载。我用下面的代码检查了日志,以及未定义或 0 的回应:

 success: function (json) {
     console.log(json);
 }

uj5u.com热心网友回复:

Enqueue Your Script and Localize 添加到functions.php:

add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_scripts' );

function my_theme_enqueue_scripts() {
    wp_enqueue_script( 'site_scripts', get_stylesheet_directory_uri() . '/scripts.js', array( 'jquery' ), '1.0', true );

    wp_localize_script(
        'site_scripts',
        'site_ajax',
        array(
            'ajax_url'    => admin_url( 'admin-ajax.php' ),
            'check_nonce' => wp_create_nonce( 'site_ajax_nonce' ),
        )
    );

}

add_action('wp_ajax_email_exists_check', 'email_exists_check');
add_action('wp_ajax_nopriv_email_exists_check', 'email_exists_check');

function email_exists_check() {
    // Check nonce and email is set by ajax post.
    if ( isset( $_POST['email'] ) && wp_verify_nonce( 'check_nonce', 'security' ) ) {
        // Sanitize your input.
        $email = sanitize_text_field( wp_unslash( $_POST['email'] ) );
        // do check.
        if ( email_exists( $email ) ) {
            $response = true;
        } else {
            $response = false;
        }
        // send json and exit.
        wp_send_json( $response );
    }
}

还有你的 jQuery:

$( document ).on(  "submit",  "#form",  function(e) {
    e.preventDefault();
    $email = $( this ).find( 'input[name=email]' ).val(); // email
    // ajax request, check if user exists.
    $.ajax({
        type: "POST",
        dataType: 'json',
        url: site_ajax.ajax_url,
        data: {
            email: $email,
            action: 'email_exists_check',
            security: site_ajax.check_nonce
            }
        })
        .done(function(data) {
            if (data.result) {
                alert( 'Email exists!' );
            } else {
                alert( 'Email doesn\'t exsist!' );
            }
        });
});

uj5u.com热心网友回复:

尝试像这样决议 JSON 回应:

 $(document).on("submit", "#form", function(e) {
     e.preventDefault();
     $email = $(this).find('input[name=email]').val(); // email
     //ajax request, check if user exists
     $.ajax({
        type: "GET",
        dataType: 'json',
        url: site_ajax.ajax_url,
        data: {
            action: 'email_exists_check',
            security: site_ajax.site_ajax_nonce
        },
        success: function(data) {
            var status = JSON.parse(data);
            if (status.result === true) {
                alert('Email exists!');
            } else {
                alert('Email doesn\t exsists!');
            }
        }
    });
});

另外,不要忘记die()在 WordPress 中呼叫 AJAX 之后

<?php 
add_action('wp_ajax_email_exists_check', 'email_exists_check');
add_action('wp_ajax_nopriv_email_exists_check', 'email_exists_check');

function email_exists_check() {

    $response = array();
    $email = $_GET['email'];

    // do check
    if ( email_exists( $email ) ) {
        $response['result'] = true;
    } else {
        $response['result'] = false;
    }

    // echo json
    echo json_encode( $response );
    die();
}

uj5u.com热心网友回复:

这是wp_verify_nonce造成问题的原因。

这就是我修改代码的方式,并且在所有方面都按预期正常作业:

$(document).on("submit", "#form", function(e) {
     e.preventDefault();
     $email = $(this).find('input[name=email]').val(); // email
     //ajax request, check if user exists
     $.ajax({
       type: "GET",
       dataType: 'json',
       url: site_ajax.ajax_url,
       data: {
         email: $email,
         action: 'email_exists_check',
         check_nonce: site_ajax.check_nonce
       },
       success: function(data) {
         if (data.result) {
           alert('Email exists!');
         } else {
           alert('Email does not exists!');
         }
       }
     });
   });

注意更换securitycheck_noncedata:

在 PHP 代码下方:

add_action('wp_ajax_email_exists_check', 'email_exists_check');
add_action('wp_ajax_nopriv_email_exists_check', 'email_exists_check');

function email_exists_check() {
    // Check nonce and email is set by ajax post.
    if (wp_verify_nonce($_POST['check_nonce'], 'site_ajax_nonce')) {
        // Sanitize your input.
        $email = sanitize_text_field(wp_unslash($_POST['email']));
        // do check.
        if (email_exists($email)) {
            $response = true;
        } else {
            $response = false;
        }
        // send json and exit.
        wp_send_json($response);
    }
}

注意if (wp_verify_nonce){}陈述句和使用nonce.

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *