0

我一直在使用 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 




?>
4

2 回答 2

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>
于 2013-09-17T17:07:51.840 回答
0

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

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

提交表单时,此输入将创建一个带有名称action和值的 POST 字段。submit您可以使用它在较高级别更改程序流程,但在使用之前仍需要使用该isset()功能检查其他字段是否存在。

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

您还有相当数量的额外内容}导致语法错误。反向缩进使发现这些错误变得更加困难。

于 2013-09-17T17:02:04.737 回答