如何验证表格和用PHP发送电子邮件

时间:2013-08-27 11:23:43

标签: php html forms

我做了一点点,但不是那么好。我想检查电子邮件是否以正确的格式有效,名称中没有数字和&另外我如何添加验证以检查数学问题是否正确。有人可以帮忙吗?

<?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>

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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检查答案。