我需要在联系表单中添加必填表单字段,但不确定如何操作?

时间:2012-09-22 17:26:17

标签: php html forms validation

我对php很新,所以我需要一些帮助,在我的联系表单中添加必填字段。我想显示一条漂亮的“错误”消息,告诉观众在必填字段中输入内容。

这是我的php脚本:

<?php
        if(isset($_POST['submit'])){

            $to = "benlevygraphics@gmail.com";
            $headers = "From: " . $_POST['email'];
            $subject = "Ben, you have been contacted...";
            $body = "Name: " . $_POST['name'] . "\nEmail: " . $_POST['email'] . "\nWebsite: " . $_POST['web'] . "\n" . "\nFavorite Piece of work: " . $_POST['favwork'] . "\n" . "\nMessage: " . $_POST['message'];

            if(mail($to, $subject, $body, $headers)){

            echo("<p class=contactformsent>".$_POST['name'].", your information was received. For your records an email has also been sent to you!</p>");

            }
            else{
               echo("<p class=contactformnotsent>".$_POST['name'].", Message delivery failed...</p>");
            }
       }

               if(isset($_POST['submit'])){

            $to = $_POST['email'];
            $headers = "From: benlevygraphics@gmail.com" ;
            $subject = "You contacted benlevywebdesign";
            $body =  $_POST['name'].",  Thank you for taking a look at my portfolio and contacting me.  I have received your contact information/message and I will get back to you as soon as I can!" . "\n" . "\nFor your records I have:" . "\nEmail: " . $_POST['email'] . "\nWebsite: " . $_POST['web'] . "\n" . "\nFavorite Piece of work: " . $_POST['favwork'] . "\n" . "\nMessage: " . $_POST['message'];

            if(mail($to, $subject, $body, $headers)){
            }
       }

?>

这是html代码:

<?php include("contactform.php"); ?>

<form id="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<fieldset>
            <p class="form">Name*
                <input type="text" name="name" id="name" size="30"/>
            </p>
            <p class="form">Email*
                <input type="text" name="email" id="email" size="30" />
            </p>...(the rest of the form html code here)

然后是最后一行:

 <p class="submit"><button name="submit" type="submit">Send</button></p>

    </form>

2 个答案:

答案 0 :(得分:0)

在服务器端,您只需检查:

if(isset($_POST['name'])) { } // only send the mail if this is true.

还有许多客户端解决方案。您也可以使用Javascript编写自己的。 您将不得不在服务器端包含检查,因为&#34;客户端安全性不起作用&#34; ..用户可以禁用Javascript并提交表单而不运行检查。

以下是进行表单验证的JS库的示例:

http://rickharrison.github.com/validate.js/

我通过Google搜索javascript form validation

找到了它

答案 1 :(得分:0)

一般情况下,如果您要在浏览器中显示或将其保存到数据库中,您应该从浏览器中转移任何输入以防止发生不良事件。

在以下行之前:

if(mail($to, $subject, $body, $headers)){

您需要在表单中添加一些验证,如下所示:

$errors = array();
$name = $_POST['name'];
$email = $_POST['email'];

if (empty($name)) {
    $errors['name'] = 'Please enter your name!';
}

if (empty($email)) {
    $errors['email'] = 'Please enter your e-mail!';
}

然后将您的电子邮件行更改为:

if(!count($errors) && mail($to, $subject, $body, $headers)){

然后代替:

echo("<p class=contactformnotsent>".$_POST['name'].", Message delivery failed...</p>");

如果邮件失败,您可以添加一个额外的错误:

$errors['sendmail'] = 'Unable to send the e-mail!';

此外,您只需查找表单本身的任何错误,并在顶部或每个输入下循环它们:

foreach ($errors as $error) {
    print $error."<br />\n";
}

当然,这只是一个开始;你一定要做一些额外的检查。例如,检查名称上的最小长度,剥离奇怪的字符等等。

<强>更新

Ben,根据您的评论,似乎您希望在POST发生之前进行验证。如果是这种情况,我建议做一些基本的jQuery验证。

以下是Basic jQuery Validation上的一个示例页面。

这是你得到的更多吗?

此外,如果您希望在提交评论后页面返回到您的表单,请确保您的表单URL具有表单的目标标记,如下所示:

<a name="comments_form" />
<form id="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>#comments_form" method="POST">