-1

我有点不知道如何正确验证此表格。实际上,它可以工作,但是当我输入正确/错误的内容时,然后按提交按钮,文本不会保存在输入字段中,这让我再次输入所有内容。我是 php 的菜鸟,这里有一些很棒的人帮助我在 PHP 中验证表单。另外,我想知道,是否可以同时使用 JS 和 php 来验证表单?如果用户禁用了 js,我将在客户端使用验证插件并在服务器上使用 php。如果您不介意请检查表单是否可以。顺便说一句,请禁用 js 以在 php 检查表单时查看问题。 https://www.dropbox.com/s/vka7cy70xv4zryf/formtest.zip 谢谢你的时间!

<title>FormTest</title>
        <link rel="stylesheet" type="text/css" href="css.css"/>

        <style type="text/css">
            .formerrors li{color:yellow;
                font-size:14px;
                font-weight:lighter;
                font-family:Tahoma, Geneva, sans-serif;}


        </style>
    </head>
    <body>
<div id="contact">
    <h1 class="heading1"> Contact:</h1>
        <form name="contact" action="" method="post">

            <label for="YourName">Your Name:</label>
            <input type="text" name="name" class="required" />

            <label for="YourEmail">Your Email:</label>
            <input type="text" name="email" class="required"/>

            <label for="Subject">Subject:</label>
            <input type="text" name="subject" class="required"  />

            <label for="YourMessage">Your Message:</label>
            <textarea  name="message" class="required"></textarea>
            <p class="c3">10 + 5 =<input type="text" name="answerbox" id="answerbox" /></p>

        <fieldset>
            <input type="submit" name="submit" id="submit" value="Send" class="required"/>
            <input type="reset" id="reset" value="Reset"/>      
        </fieldset>
<?php
    if(isset($_POST['submit'])){
    $name = trim($_POST["name"]);
    $email = trim($_POST["email"]);
    $subject = trim($_POST["subject"]);
    $message = trim($_POST["message"]);
    $answerbox = trim($_POST["answerbox"]);
    if(empty($_POST['name'])){
        echo "<div class='formerrors'><li>Please type your name.</li></div>";
    } else {
        if (ctype_alpha($name) === false) {
        echo "<div class='formerrors'><li>Your name only contain letters!</li></div>";
        }
    }


    if(empty($_POST['email'])){
        echo "<div class='formerrors'><li>You've forgot to type your email address.</li></div>";
    } else{
        if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
            echo "<div class='formerrors'><li>Your email is not valid, please check.</li></div>";
        }
    }
    if(empty($_POST['subject'])){
        echo "<div class='formerrors'><li>Please type a subject.</li></div>";
    }
    if(empty($_POST['message'])){
        echo "<div class='formerrors'><li>You've forgot to type your message.</li></div>";
    }
    if(empty($_POST['answerbox'])){
        echo "<div class='formerrors'><li>Please answer the math question.</li></div>";
    }   else
    if($answerbox != 15){
            echo "<div class='formerrors'><li>Answer is not correct.</li></div>";
    }
    else{
        $headers =  'From: '.$email. "\r\n" .
        'Reply-To: '.$email . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
        mail('me@mymail.me',$subject,$message,$headers);
        print "Your message was sent successfully";
    }
}


?>
    </form>
</div><!--contact-->

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="jquery.validate.min.js"></script>
<script type="text/javascript">
            $(document).ready(function() {
                jQuery.validator.addMethod("lettersonly", function(value, element) 
                            {return this.optional(element) || /^[a-z]+$/i.test(value);
                        }, "Letters only please.");

                      $("form").validate({

                        rules:{
                            name: {required: true,
                                maxlength:30,
                                lettersonly: true,},


                            email: {required: true,
                                email: true,
                                minlength: 5,
                                maxlength:40},              

                        answerbox: {required: true,
                                max: 15,
                                min: 15},
                        },

                      });
                    });

</script>


    </body>
</html>
4

2 回答 2

0

当站点重新加载时,表单被清除。

您必须自己填写。

如果您有这样的输入字段:

<input type="text" name="test" id="test" />

你可以这样做:

<input type="text" name="test" id="test" value="<?= isset($_POST['test']) ? $_POST['test'] : '' ?>" />

另一个已经设定值的例子:

<input type="text" name="test2" id="test2" value="Hello" />

变成:

<input type="text" name="test2" id="test2" value="<?= isset($_POST['test2']) ? $_POST['test2'] : 'Hello' ?>" />

这将检查是否设置了 POST 字段(在提交表单时发生),然后设置表单字段,否则为空或默认值。

您还可以通过额外检查表单是否无效来扩展此功能,然后填写字段,否则不填写。

文本区域示例:

<textarea name="testtxt" id="testtxt"></textarea>

变成:

<textarea name="testtxt" id="testtxt"><?= isset($_POST['testtxt']) ? $_POST['testtxt'] : '' ?></textarea>
于 2013-08-29T11:09:47.350 回答
0

名称:输入类型=“文本”名称=“名称”值= php echo $name;

保持价值:--

value=?php 回显 $name;?

于 2015-08-20T10:30:35.123 回答