PHP表单:验证,提交,显示内容

时间:2012-03-01 15:44:19

标签: php forms validation error-handling

在过去的几天里,我一直在尝试修改我在互联网上找到的PHP表单(我可以看到它非常受欢迎,因为我已经看过很多关于该表单的帖子)。 提交表单时,它会给我发一封电子邮件 - 这很棒。但是我还有其他一些想要改变的东西,根本无法找到合适的/可行的解决方案。

我想要实现的目标是:

1)在提交表单时收到电子邮件(我认为这样做了)

2)验证

3)在同一页面内显示错误消息。

姓名*

(您的姓名是必需的)

[.......这是文字字段.......]

4)允许只提交一次表格 - 当所有错误得到纠正或第一次正确输入所有数据时。

5)在验证数据时出现错误消息(之前)时,我的页脚未显示。这是什么原因?表单提交后显示正常。

这是我一直试图修改的代码(我复制了'原始'代码,因为我玩它后可能太乱了,太改了)

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

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "you@yourdomain.com";
$email_subject = "Your email subject line";


function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
}

// validation expected data exists
if(!isset($_POST['first_name']) ||
    !isset($_POST['last_name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['telephone']) ||
    !isset($_POST['comments'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');       
}

$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required

 $error_message = "";
 $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
 if(!preg_match($email_exp,$email_from)) {
 $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
 }
 $string_exp = "/^[A-Za-z .'-]+$/";
 if(!preg_match($string_exp,$first_name)) {
 $error_message .= 'The First Name you entered does not appear to be valid.<br />';
 }
 if(!preg_match($string_exp,$last_name)) {
 $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
 }
 if(strlen($comments) < 2) {
 $error_message .= 'The Comments you entered do not appear to be valid.<br />';
 }
 if(strlen($error_message) > 0) {
 died($error_message);
 }
 $email_message = "Form details below.\n\n";

 function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

<!-- include your own success html here -->

Thank you for contacting us. We will be in touch with you very soon.

<?php
}
?>

以下是我的代码的一部分来自contact.php(表格在哪里)

<form name="contactform" method="post" action="submit_form.php">

<div class="left_form">
 <label for="name" class="label">your name</label>

(this is the code that was suppose to display error message within the same page

<?php if($errors['name'] = "") echo $errors['name']; ?>)


<input type="text" class="inputtext" name="name" value="" />

以上代码对于所有输入字段几乎相同(唯一不同的是name =“”)

如果有人能帮助我,我将非常感激。

谢谢。 =)

2 个答案:

答案 0 :(得分:1)

从函数中处理帖子并生成一个单独的数组来保存错误字段。如果您有错误,请输出它们,或者如果验证的字段确定,则处理帖子。永远不要相信用户输入。

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

    if(!ctype_alpha($_POST['fieldname'])) //or other validation type you prefer
    {
       $errors[] ='<p>problem with fieldname</p>';
    }

... repeat above for each field

    if(count($errors))
    {
        //output error message using foreach from
    }
    else
    {
        //process the post
    }
}

如果您对php不熟悉,我建议您进行一些繁重的阅读和辅导学习,这样您就可以了解它是什么,以及将来如何改进它。

首先以纯文本形式写出你想要作为一种伪代码形式发生的事情然后一点一点地用真正的PHP代码替换,这样你就可以在每个阶段测试你的结果。

php official site foreach construct

答案 1 :(得分:0)

我强烈建议您使用表单库来消除痛苦

以下是一些例子:

如果您仍想自己验证和清理用户输入,请至少尝试filter php extension。但实际上,尝试重用外部代码而不是重新发明轮子,它会清理你的代码很多!!!!

此外,您的代码至少会遇到安全问题CSRF request forgery,这意味着有人可能会在未经他同意的情况下提交此表单