在我的 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!');
}
}
});
});
注意更換security同check_nonce在data:。
在 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.
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/397271.html
標籤:php 查询 阿贾克斯 WordPress的
