0

我对 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>
4

2 回答 2

0

在服务器端,您可以检查:

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

还有许多客户端解决方案。您也可以使用 Javascript 编写自己的代码。由于“客户端安全性不起作用”,您仍然必须在服务器端包含检查。用户可以禁用 Javascript 并在不运行检查的情况下提交表单。

这是一个执行表单验证的 JS 库的示例:

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

我通过谷歌搜索找到了javascript form validation

于 2012-09-22T17:36:11.733 回答
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 验证。

这是一个带有示例的基本 jQuery 验证页面。

这就是你要说的吗?

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

<a name="comments_form" />
<form id="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>#comments_form" method="POST">
于 2012-09-22T17:37:42.347 回答