0

我正在使用 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.';
}
?>
4

1 回答 1

0

根据Remote Method 的文档,错误消息可以是您从服务器返回的任何内容。

响应被评估为 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

于 2013-09-18T00:47:51.620 回答