我是ajax的新手。我已经从functions.php
中获得了这个php函数
function checkUserEmailExistent($email){
...
return $boolean;
}
这是我的看法views.html
<input type='text' name='email' id='email'>
这是给script.js
的
jQuery( "#email" ).blur(function() {
jQuery.ajax({
type: 'POST',
url: 'url',
dataType: 'json',
data: { 'value' : $(this).val() },
success : function(result){
}
});
});
我的问题是如何在ajax中调用php函数,将其连接到我的html。当它blur
时,它检查电子邮件值是否存在。
在WordPress 中工作
JS脚本
jQuery( "#email" ).blur(function() {
jQuery.ajax(
{
url: ajax_url,
type: "POST",
dataType: "json",
data: {
action: 'checkUserEmailExistent',
email: $(this).val(),
},
async: false,
success: function (data)
{
if (data.validation == 'true')
jQuery('.email-massage').html('<div class="alert alert-success"><a href="#" class="close" data-dismiss="alert">×</a><strong>Success!</strong> successfully</div>');
else
jQuery('.email-massage').html('<div class="alert alert-danger"><a href="#" class="close" data-dismiss="alert">×</a><strong>Oops!</strong> Something went wrong.</div>');
},
error: function (jqXHR, textStatus, errorThrown)
{
jQuery('.email-massage').html('<div class="alert alert-danger"><a href="#" class="close" data-dismiss="alert">×</a><strong>Oops!</strong> Something went wrong.</div>');
}
});
});
WP功能脚本.pp
add_action('wp_ajax_checkUserEmailExistent', 'checkUserEmailExistent');
add_action('wp_ajax_nopriv_checkUserEmailExistent', 'checkUserEmailExistent');
function checkUserEmailExistent() {
$email = $_POST['email']; // get email val
/*if() your condition
$email = 1;
else
$email = 0;
*/
if ($email == 1):
$email_val= 'true';
else:
$email_val = 'false';
endif;
echo json_encode(array("validation" => $email_val));
die;
}
在function.php Enqueue文件中添加类似的代码后
wp_enqueue_script('themeslug-default', get_template_directory_uri() . '/js/default.js', array('jquery'));
wp_localize_script('themeslug-default', 'ajax_url', admin_url('admin-ajax.php'));
将url设置为具有checkUserEmailExistent函数的php文件。然后:
function checkUserEmailExistent($email){
...
return $boolean;
}
return checkUserEmailExistent($_REQUEST['value']);
我给出了验证的示例。这将帮助您检查
Email id<input type="text" name="email" id="email" size=18 maxlength=50 onblur="javascript:myFunction(this.value)">
您需要添加脚本
<script>
function myFunction(em) {
if(em!='')
{
var x = document.getElementById("email").value;
var atpos = x.indexOf("@");
var dotpos = x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
alert("Not a valid e-mail address");
document.getElementById("email").value = "";
return false;
exit();
}
var email=$("#email").val();
$.ajax({
type:'post',
url:'email_client.php',
data:{email: email},
success:function(msg){
if (msg.length> 0) {
alert(msg);
document.getElementById("email").value = "";
}
}
});
} }
</script>
创建一个页面"email_client.php"并添加代码
<?php
$s=$_POST['email'];
include "config.php";
$echeck="select email from client where active=0 and email='".$_POST['email']."'"; //change your query as you needed
$echk=mysql_query($echeck);
$ecount=mysql_num_rows($echk);
if($ecount>='1' && $s!='0')
{
echo "Email already exists";
}
?>
您可以在url
参数中调用它。但是,您需要在PHP脚本中管理AJAX处理程序。
AJAX
jQuery( "#email" ).blur(function() {
jQuery.ajax({
type: 'POST',
url: 'functions.php',
dataType: 'json',
data: { 'value' : $(this).val() },
success : function(result){
if (result.success) {
//handle success//
} else if (result.failure) {
//handle failure//
}
}
});
});
PHP
function checkUserEmailExistent($email){
...
return $boolean;
}
if ($_POST['value']) {
$status = checkUserEmailExistent($email);
if ($status === true) {
echo json_encode (array('status' => 'success'));
} elseif ($status === false) {
echo json_encode (array('status' => 'failure'));
}
}
您没有在Ajax内部调用服务器函数,您只在获取数据时以JSON格式将数据发送到服务器,服务器会将其路由(如果是MVC)到特定函数,并以JSON格式向客户端返回响应,因此现在在Ajax内部,您可以在成功时执行操作(下一步该怎么做),如果失败,则显示错误
服务器将如何将其路由到特定的函数,这取决于您使用的框架,但我认为他们只是使用regexp来匹配URL