-3

我做了一点点,但不是很好。我想检查电子邮件是否以正确的格式有效,名称是否不包含数字以及如何添加验证以检查数学问题是否正确。有人可以帮忙吗?

<?php
    $submitted = $_POST["submitted"];
    if($submitted == "true")
    {
    $name = trim($_POST["name"]);
    $email = trim($_POST["email"]);
    $subject = trim($_POST["subject"]);
    $message = trim($_POST["message"]);
    $answerbox = trim($_POST["answerbox"]);
    if($name == "") print "<p>Please type your name.</p>";
    if($subject == "") print "<p>Please type a subject.</p>";
    if($email == "") print  "<p>Please type a valid email address.</p>";
    if($message == "") print "<p>Please type your message.</p>";
    if($answerbox == "") print "<p>Please answer the math question.</p>";
    }

?>
        <form name="contact" action="form2.php" method="post">
            <input type="hidden" name="submitted" value="true"/>
            <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>

    </form>
4

2 回答 2

1
<?php
    if(isset($_POST['name']))
    {
    $name = trim($_POST["name"]);
    $email = trim($_POST["email"]);
    $subject = trim($_POST["subject"]);
    $message = trim($_POST["message"]);
    $answerbox = trim($_POST["answerbox"]);
    if(strlen($name)<2) {
        print "<p>Please type your name.</p>";
    }else if(strlen($subject)<2) {
        print "<p>Please type a subject.</p>";
    }else if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        print  "<p>Please type a valid email address.</p>";
    }else if(strlen($message)<10) {
        print "<p>Please type your message.</p>";
    }else if($answerbox != 15) {
        print "<p>Please answer the math question.</p>";
    }else{
                $headers =  'From: '.$email. "\r\n" .
                            'Reply-To: '.$email . "\r\n" .
                            'X-Mailer: PHP/' . phpversion();
        mail('me@mymail.me',$subject,$message,$headers);
        print "mail succesuffully sent";
    }

}
    ?>

        <form name="contact" action="form2.php" method="post">
            <input type="hidden" name="submitted" value="true"/>
            <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>

    </form>
于 2013-08-27T11:39:46.580 回答
0

验证电子邮件的最佳、最安全和最简单的方法如下:

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // invalid emailaddress
}

至于其他答案(尤其是数学答案),在允许提交表单之前,您最好直接在页面上使用一些 javascript 来验证答案吗?

数学可以这样完成:

if(empty($answerbox)) 
{
    echo "<p>Please answer the math question.</p>";
}
elseif ($answer!=15)
{
    echo "Oh oh, the answer seems wrong...";
}
else
{
    // Something else?
}

我认为您对 PHP 在何处以及如何运行的理解也可能不正确。所有 PHP 代码在到达用户之前就在服务器上执行。在您提交表单并在服务器上处理它之前,您无法通过 PHP 检查答案。

于 2013-08-27T11:25:28.740 回答