簡體   English   中英

HTML PHP將表單重定向提交到同一頁面,並顯示錯誤消息

[英]HTML PHP redirect submit form to same page with error message

我正在創建一個提交聯系表。 但是,當按下帶有錯誤信息的提交按鈕時,我希望它返回到我的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);}

但是,當我輸入不正確的名稱時,將不會出現錯誤消息。

你能幫我嗎?

問候,Lex

我認為您正在嘗試使用$_GET 如果是這樣,您可以執行以下操作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM