我正在使用jquery验证插件。我有一个电子邮件字段。如果电子邮件正在使用中,我希望验证插件给出一个错误,其中包含指向客户端页面的链接。
IE:如果我输入电子邮件 john@bobo.com 并且他是名为John Mark的客户端#312,我希望我的错误是:
<a href="/index.php?client=312">John Mark</a> is using that email.
最好让我的外部文件只回显整个错误,并让 jQuery 验证插件显示完整的错误。如果没有,我希望同时返回客户端名称和客户端 ID,然后能够输出带有链接的错误消息。
j查询代码:
$().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")
},
}
});
});
形式:
<form action="#" method="post" id="sign-up_area">
<h2 id="reference" name="reference" class="heading-reference">Client</h2>
<fieldset>
<label for="firstName">First Name:</label>
<input type="text" name="firstName" id="firstName" value="">
</fieldset>
<fieldset>
<label for="lastName">Last Name:</label>
<input type="text" name="lastName" id="lastName" value="">
</fieldset>
<fieldset>
<label for="email_address">Email:</label>
<input type="text" name="email_address" id="email_address" value="">
<div id="email" class="error"></div>
</fieldset>
<fieldset class="form-actions">
<input type="submit" value="Submit">
</fieldset></form>
数据库检查.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、未定义或 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
}
请参阅:http://php.net/manual/en/function.json-encode.php