协助PHP表单验证

时间:2013-09-17 16:59:03

标签: php regex forms validation email

我一直在使用php页面将邮件发送到电子邮件地址并且一直在尝试添加验证。我在某个地方出了问题,但我不确定在哪里?该代码应该在按下提交按钮后开始验证。它应该检查一个字段是否为空然后验证设置正则表达式然而我认为我犯了一个错误。如果表单已成功验证,则会将其发送到我的电子邮箱。

<?php 

if(isset($_POST{'submit'} )){

    $f_name=$_POST['first_name'];
    $l_name=$_POST['last_name'];
    $email=$_POST['email']; 
    $message=$_POST['message']; 

    $to = "email@somewhere.com";
    $subject = "A message has been sent from your site";
    $comment = "You have been emailed by: $email \n
                Message Contents: $message \n
                Message sent by $f_name $l_name \n
                Email to respond to: $email" ;

    if (empty($_POST["f_name"]))
        echo "Please enter your first name";
        }
    else{
        if (!preg_match("/^[a-zA-Z ]*$/",$f_name))
            {
        echo "Your first name does not appear to be valid";
            }
    }

    if (empty($_POST["l_name"]))
        echo "Please enter your last name";
        }
    else{
        if (!preg_match("/^[a-zA-Z ]*$/",$f_name))
            {
        echo "Your last name does not appear to be valid.";
            }
    }

    if (empty($_POST["email"]))
        echo "Please enter your email";
        }
    else{
        if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
            {
        echo "Your email does not appear to be valid.";
            }
    }

    if (empty($_POST["message"]))
        echo "Please enter a message";
        }

        else{
                    mail($to, $subject, $message);

                    /* Print a message for the user */
                    echo "<b>Your message has been sent successfully, details are as followed:</b><br />";
                    echo "<br /><b>First name: </b>".$f_name;
                    echo "<br /><b>Last name: </b>".$l_name;
                    echo "<br /><b>Email: </b>".$email;
                    echo "<br /><b>Comments: </b>".$message;
                    echo "<br /><br /><b>Thank you for your comment.</b>"; 
    }else{
    ?> 
    <form  action="contact_page.php" method="POST" enctype="multipart/form-data"> 
    <input type="hidden" name="action" value="submit"> 
    First Name:<br> 
    <input name="first_name" type="text" value="" size="30"/><br> 
    Surname:<br>
    <input name="last_name" type="text" value="" size="30"/><br> 
    Your email:<br> 
    <input name="email" type="text" value="" size="30"/><br> 
    Your message:<br> 
    <textarea name="message" rows="7" cols="30"></textarea><br> 
    <input type="submit" value="Send email" name="submit"/> 
    <input type="reset" value="Reset Form">
    </form> 
    <?php 




?>

2 个答案:

答案 0 :(得分:1)

你到处都没有爆发,我相信问题在于

if (empty($_POST["message"])){
    echo "Please enter a message";
    }else{
                mail($to, $subject, $message);

                /* Print a message for the user */
                echo "<b>Your message has been sent successfully, details are as followed:</b><br />";
                echo "<br /><b>First name: </b>".$f_name;
                echo "<br /><b>Last name: </b>".$l_name;
                echo "<br /><b>Email: </b>".$email;
                echo "<br /><b>Comments: </b>".$message;
                echo "<br /><br /><b>Thank you for your comment.</b>"; 
}

开始:按照你的花括号,我认为你不在一些,但最终,你的验证方式使它除了消息之外一切都是空白的。因此,验证器只检查消息块并执行电子邮件。

我会让自己变得更容易一些。

<div id="message">
<?php
if ( isset($_POST['first_name']) || isset($_POST['last_name') || [ETC.]){
     echo("I'm sorry, but all fields must be filled in.");
}else{
     mail($to, $subject, $message);

     /* Print a message for the user */
     echo "<b>Your message has been sent successfully, details are as followed:</b><br />";
     echo "<br /><b>First name: </b>".$f_name;
     echo "<br /><b>Last name: </b>".$l_name;
     echo "<br /><b>Email: </b>".$email;
     echo "<br /><b>Comments: </b>".$message;
     echo "<br /><br /><b>Thank you for your comment.</b>";
}
?>
</div>

答案 1 :(得分:0)

我认为您的意思是if(isset($_POST['action']))而不是if(isset($_POST{'submit'} ))

<input type="hidden" name="action" value="submit">

在提交表单时,此输入将创建名称为action且值为submit的POST字段。您可以使用它来更高级别更改程序流,但仍然需要在使用之前使用isset()函数检查是否存在其他字段。

如果$_POST['action']存在但$_POST['first_name']不存在,您的脚本会发出通知。

在导致语法错误的过程中,您还有相当多的额外}。反向缩进使得发现这些错误变得更加困难。