【问题标题】:PHP form : validation, submission, displaying contentPHP 表单:验证、提交、显示内容
【发布时间】:2012-03-20 02:58:53
【问题描述】:

在过去的几天里,我一直在尝试修改我在 Internet 上找到的 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="")

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

谢谢。 =)

【问题讨论】:

    标签: php forms validation error-handling


    【解决方案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

    【讨论】:

    • 嗨。感谢您的时间。 :-) 我一直在尝试实施您的答案。我忘了提到我对 PHP 不是很熟悉。 :-P(到目前为止,我一直在使用我的复制>粘贴>研究>编辑方法进行管理)。我不确定如何正确编写 foreach 表单。另外,我想要使用您的代码,我应该从旧代码中删除一些部分(这样它们就不会发生冲突?),它们应该是哪些?很抱歉成为一个痛苦的人。 :-P
    【解决方案2】:

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

    这里有一些例子:

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

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

    【讨论】:

      猜你喜欢
      • 2014-04-23
      • 1970-01-01
      • 2012-11-19
      • 1970-01-01
      • 1970-01-01
      • 2016-02-14
      • 2023-03-07
      • 2016-02-05
      • 1970-01-01
      相关资源
      最近更新 更多