在我的 WordPress v5.8.2 中,我ajax_url在functions.php 中本地化了:
wp_localize_script('site_admin_ajax', '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: {
action: 'email_exists_check',
security: site_ajax.site_ajax_nonce
},
success: function(data) {
if (data.result) {
alert('Email exists!');
} else {
alert('Email doesn't exsists!');
}
}
});
});
在單獨檔案中的 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() {
$email = $_GET['email'];
// do check
if (email_exists($email)) {
$response - > result = true;
} else {
$response - > result = false;
}
// echo json
echo json_encode($response);
}
如果電子郵件存在,則上述整個代碼無法發出警報。
我已經在form_email_exists_check()沒有 ajax的情況下測驗了代碼并且作業正常。我怎樣才能使這段代碼作業?
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熱心網友回復:
您的入隊腳本沒問題,但是您在 Ajax 和 PHP 中遇到了一些問題。沒有你的完整實作,我無法測驗它,但它應該可以作業。
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 );
}
}
還有你的 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!' );
}
});
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/395427.html
標籤:php 查询 阿贾克斯 WordPress的
上一篇:如何填充半圓的上部?
