【问题标题】:HTML PHP redirect submit form to same page with error messageHTML PHP 将提交表单重定向到带有错误消息的同一页面
【发布时间】:2014-03-05 15:14:48
【问题描述】:

我正在创建一个提交联系表单。但是,当使用不正确的信息按下提交按钮时,我希望它返回到我的contact.php 页面,并在正文页面顶部显示附加错误消息,例如。 “我们很抱歉,您提交的表格有错误,这些错误出现在下面: 不正确的电子邮件 不完整的姓氏”等

我有两个文件,contact.php 用于表单,send_form_email.php 用于电子邮件处理

在我的联系人文件中,我有以下代码;

$inc = $_GET['inc'];
if($inc ==1) {
            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 />";}
    ?>

在我的 send_form_email.php 中,我有以下代码

function died($error) {
    header("location: contact.php?inc=1"); 
    die();
}

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);}

但是,当我输入错误的名字时,错误信息不会出现。

你能帮帮我吗?

问候,莱克斯

【问题讨论】:

  • 什么是died?可能是出了什么问题。在此处查看示例代码:ideone.com/CZp0pz
  • echo $error."

    "; >> 此代码在contact.php 中不起作用。其余的都在起作用。
  • 你在测试什么名字?

标签: php html forms


【解决方案1】:

我认为您正在尝试使用$_GET。如果是这样,您可以执行以下操作:

if($_GET['inc'] ==1) 

【讨论】:

  • 嗨,抱歉,我已经添加了上面提到的代码,但是 $error 仍然不起作用。
猜你喜欢
  • 2020-10-12
  • 2013-01-15
  • 2012-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-04
  • 1970-01-01
相关资源
最近更新 更多