我正在使用 jquery 验证插件。我有一个电子邮件字段。如果电子邮件正在使用中,我希望验证插件给出错误并带有指向客户端页面的链接。
IE:如果我输入电子邮件 john@bobo.com 并且他是名为 John Mark 的客户 #312,我希望我的错误是:
<a href="/index.php?client=312">John Mark</a> is using that email.
最好我想让我的外部文件只回显整个错误并让 jQuery Validation 插件显示该完整错误。如果没有,我希望同时返回客户端名称和客户端 ID,然后能够通过链接输出错误消息。
jQuery代码:
$().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.';
}
?>