Php jQuery验证错误返回链接
我正在使用jquery验证插件。我有一个电子邮件字段。如果电子邮件正在使用中,我希望验证插件给出一个错误,并带有指向客户端页面的链接 IE:如果我输入电子邮件john@bobo.com他是312号客户,名叫约翰·马克,我希望我的错误是: 他正在使用那封电子邮件 最好是让我的外部文件只回显整个错误,并让jQuery验证插件显示完整的错误。如果没有,我希望返回客户端名称和客户端id,然后能够输出带有链接的错误消息 jQuery代码:Php jQuery验证错误返回链接,php,jquery,jquery-validate,Php,Jquery,Jquery Validate,我正在使用jquery验证插件。我有一个电子邮件字段。如果电子邮件正在使用中,我希望验证插件给出一个错误,并带有指向客户端页面的链接 IE:如果我输入电子邮件john@bobo.com他是312号客户,名叫约翰·马克,我希望我的错误是: 他正在使用那封电子邮件 最好是让我的外部文件只回显整个错误,并让jQuery验证插件显示完整的错误。如果没有,我希望返回客户端名称和客户端id,然后能够输出带有链接的错误消息 jQuery代码: $().ready(function() { // Val
$().ready(function() {
// Validate the form
$('#sign-up_area').validate({
rules: {
firstName: "required",
lastName: "required",
email_address: {
email: true,
remote: {
url: "includes/databasecheck.php",
type: "post",
success: function(html){
$("#email").html(html);
}
}
}
},
messages: {
firstName: "First Name Required",
lastName: "Last Name Required",
email_address: {
email: "Email address need to be valid.",
//remote: jQuery.format("{0} is taken")
},
}
});
});
表格:
databasecheck.php
<?php
include_once("config.php"); // database connection
if(isset($_POST['email_address'])) {
$stmt = $mysqli->prepare("SELECT client_id, CONCAT(firstName, ' ', lastName) AS whole_name FROM client WHERE email = ? LIMIT 1");
$stmt->bind_param('s', $_POST['email_address']);
$stmt->execute(); // Execute the prepared query.
$stmt->store_result();
$stmt->bind_result($client_id, $whole_name);
$stmt->fetch();
echo '<a href="/index.php?client='.$client_id.'">'.$whole_name.'</a> is using that email.';
}
?>
根据,错误消息可以是您从服务器返回的任何内容
响应被评估为JSON,对于有效元素,响应必须为true;对于无效元素,响应可以为false、undefined或null,使用默认消息;或字符串,例如,该名称已被采用,请尝试使用peter123显示为错误消息
只要您将服务器上的自定义错误消息构造为有效的JSON,您的代码就可以处理远程删除的消息
rules: {
firstName: "required",
lastName: "required",
email_address: {
email: true,
remote: {
url: "includes/databasecheck.php",
type: "post",
success: function(html){
$("#email").html(html);
}
}
}
},
messages: {
firstName: "First Name Required",
lastName: "Last Name Required",
email_address: {
email: "Email address need to be valid."
// The remote error message is coming from the server automatically
// remote: jQuery.format("{0} is taken") // <- REMOVE
},
}
试试这个PHP:
if(isset($_POST['email_address'])) {
$stmt = $mysqli->prepare("SELECT client_id, CONCAT(firstName, ' ', lastName) AS whole_name FROM client WHERE email = ? LIMIT 1");
$stmt->bind_param('s', $_POST['email_address']);
$stmt->execute(); // Execute the prepared query.
$stmt->store_result();
$stmt->bind_result($client_id, $whole_name);
$stmt->fetch();
$response = '<a href="/index.php?client='.$client_id.'">'.$whole_name.'</a> is using that email.';
echo json_encode($response); // failed validation- show the message
} else {
echo "true"; // passed validation- no message
}
请参阅:不幸的是,当我回显除true以外的任何内容时,会得到默认错误消息或在消息下的jquery代码中定义的错误消息。我无法获得我在上面发布的所需错误。因此,我无法传回多个值。而且我无法获取包含html代码的错误代码。@ShawnDibble,只要它是有效的JSON,它就应该像广告一样工作。见:谢谢。它现在正按预期工作。我已经做了两天了,你在15分钟内就解决了。奇怪的错误出现了。1此表单将不生效。2当它第一次显示数据库中的错误时,它会给我一个链接,但是当我退出时,它会一直粘住,直到我给出一个无效的电子邮件地址。同样在这一点上,如果我发送另一封已经在使用的电子邮件,我不能在模糊该字段的那一刻单击链接,错误立即消失。@ShawnDibble,这听起来像是一个全新的问题。请将其作为新问题发布,因为“评论”不是进行此类讨论的好地方。
if(isset($_POST['email_address'])) {
$stmt = $mysqli->prepare("SELECT client_id, CONCAT(firstName, ' ', lastName) AS whole_name FROM client WHERE email = ? LIMIT 1");
$stmt->bind_param('s', $_POST['email_address']);
$stmt->execute(); // Execute the prepared query.
$stmt->store_result();
$stmt->bind_result($client_id, $whole_name);
$stmt->fetch();
$response = '<a href="/index.php?client='.$client_id.'">'.$whole_name.'</a> is using that email.';
echo json_encode($response); // failed validation- show the message
} else {
echo "true"; // passed validation- no message
}