-2

ユーザーがフィールドからユーザー名を入力すると、process.phpから検証され、検証後、名前フィールドの前に結果が表示されるようにしたいと思います。ユーザーがcontryからukを選択すると、uk cityが表示され、インドを選択した場合は、jqueryでどのように可能であるかをインドの都市に表示し、フィールドがあればエラーを表示するようにします。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html>
<head>
    <title>JQuery Form Example</title> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $("#myform").validate({
            debug: false,
            rules: {
                name: "required",
                email: {
                    required: true,
                    email: true
                }
            },
            messages: {
                name: "Please let us know who you are.",
                email: "A valid email will help us get in touch with you.",
            },
            submitHandler: function(form) {
                // do other stuff for a valid form
                $.post('process.php', $("#myform").serialize(), function(data) {
                    $('#results').html(data);
                });
            }
        });
    });
    </script>
    <style>
    label.error { width: 250px; display: inline; color: red;}
    </style>
</head>
<body>
<form name="myform" id="myform" action="" method="POST">  
<!-- The Name form field -->
    <div id='name'> </div>
    <label for="name" id="name_label">Username</label>  
    <input type="text" name="name" id="name" size="30" value=""/>  
    <br>
<!-- The Email form field -->
    <div id='email'> </div>
    <label for="email" id="email_label">Email</label>  
    <input type="text" name="email" id="email" size="30" value=""/> 
    <br>
<!-- The Country form field -->
    <div id='Select_option'> </div>
    <label for="country" id="country_label">Contry</label>  
<select name="contry">
<option value="uk">uk</option>
<option value="india">india</option>
</select>

    <br>
<!-- The Country form field -->
    <div id='uk_city'> </div>
    <label for="ukcity" id="ukcity_label">uk city</label>  
<select name="uk_city">
<option value="u0">u0</option>
<option value="u1">u1</option>
</select>

    <br>
<!-- The Country form field -->
    <div id='ind_city'> </div>
    <label for="indcity" id="indcity_label">ind city</label>  
<select name="ind_city">
<option value="i0">i0</option>
<option value="i1">i1</option>
</select>

    <br>


<!-- The Submit button -->
    <input type="submit" name="submit" value="Submit"> 
</form>
<!-- We will output the results from process.php here -->
<div id="results"><div>
</body>
</html>
4

1 に答える 1

1

まず、ほとんどの検証は、空のフィールドや無効な入力 (年齢フィールドの文字) などの簡単なことに対してサーバーのラウンドトリップを節約するために js で行う必要があります。

次に、ajax を使用してフォームを検証し、success = true または success = false を含む json オブジェクトと、エラーが発生したフィールドとエラーの内容を示すオブジェクトを含む配列を返します。

代わりに $.ajax コマンドを使用してフォーム bu を投稿することを避け、検証が必要なフィールドのみを送信することができます。

また、js の検証を信頼しないことも忘れないでください。改ざんの影響を受けないのはサーバー側の検証のみです。

通常、logged in を true に設定してサーバー側セッションを作成し、ユーザーがログインをバイパスして特定のページに直接アクセスできないようにします。

于 2012-05-18T09:32:40.427 に答える