jQueryvalidation错误返回链接

我正在使用jquery Validation Plugin。 我有一个电子邮件字段。 如果电子邮件正在使用中,我希望validation插件通过指向客户端页面的链接给出错误。

IE:如果我输入电子邮件john@bobo.com并且他是名为John Mark的客户端#312,我希望我的错误是:

John Mark 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") }, } }); }); 

形成:

 

Client

databasecheck.php

 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 ''.$whole_name.' 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 = ''.$whole_name.' 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