簡體   English   中英

PHP:如果表單中的必填字段未填寫,如何使用戶停留在同一頁面上?

[英]PHP: How to make the user stay on the same page if the required fields in the form aren't filled?

如果表單中的必填字段未填寫,如何使用戶停留在同一頁面上? 任何答案將不勝感激。

<?php 
header('Location: COMING SOON.html');
$name = $_POST['name'];
$surname = $_POST['last_name'];
$cons = $_POST['cons'];
$password = $_POST['pass'];
$email = $_POST['email'];
$message = $_POST['message'];
$recipient = "pixiedustmed@yahoo.com";

$to = 'pixiedustmed@yahoo.com';

$subject = "Contact Message from: $name $surname - $email";

$headers = "From: $name $surname \r\n";
$headers .= "Reply-To: ". strip_tags($_POST['email']) . "\r\n";
$headers .= "CC: avedis@avedis.ga\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$message = '<html><body>';
$message .= '<div class="about-center"><h1 style="font-family: Lato-Light; font-size: 2em">You have received a contact message from Your website.</h1></div>';
$message .= '<table rules="all" style="border-color: #666; font-family: Segoe UI; font-size: 16px;" cellpadding="12">';
$message .= "<tr style='background: #eee;'><td><p style='color: #2c3e50; font-weight:bold'>Name: </p> </td><td>" . strip_tags($_POST['name']) . "</td></tr>";
$message .= "<tr style='background: #eee;'><td><p style='color: #2c3e50; font-weight:bold'>Surname: </p> </td><td>" . strip_tags($_POST['last_name']) . "</td></tr>";
$message .= "<tr><td><p style='color: #2c3e50; font-weight:bold'>Email: </p> </td><td>" . strip_tags($_POST['email']) . "</td></tr>";
$message .= "<tr style='background: #71bdf4;'><td><p style='color: #ffffff'>Message: </p> </td><td>" . strip_tags($_POST['message']) . "</td></tr>";
$message .= "<tr style='background: #34495e;'><td><p style='color: #ffffff'>Consultation Info: </p> </td><td style='color: #ffffff'>" . strip_tags($_POST['cons']) . "</td></tr>";
$message .= "<tr><td><p style='color: #2c3e50; font-weight:bold'>Password: </p> </td><td>" . strip_tags($_POST['pass']) . "</td></tr>";
$message .= "</table>";
$message .= '<br><h1 style="font-family: Lato-Light; font-size: 14px; color:#c0392b;"><a href="http://avedis.ga">Click here to open avedis.ga</a></h1>';
$message .= '<h1 style="font-family: Segoe UI; font-size: 13px; color:#eee;">This is an autogenerated message designed by S.G.</h1>';
$message .= '<div class="about-center">-</div>';
$message .= "</body></html>";


mail($recipient, $subject, $message, $headers, $password) or die("Error!");
echo "Thank You!";
?>

因此,如您所見,我已經放置了header('Location: COMING SOON.html'); 這是行得通的。 另外,在我的CSS中,我放置了一個required=""字段,該字段不允許用戶單擊“提交”按鈕並顯示一條消息。 但是,既然我放了頭文件, PHP忽略一切。

單擊此處可以訪問我的網站; 向下滾動到頁面末尾,然后嘗試使用聯系表,然后。 讓我知道您對設計及所有方面的看法!

if(empty($name) || empty($surname) || empty($cons) || empty($password) || empty($email) || empty($_POST['email']) || empty($message)){
    // do something if a field is empty
} else {
    // do something if fields are all filled
}

在聲明諸如$name$surname$cons類的變量之后,將其放入。

使用一些JavaScript來確保用戶填寫了所有字段,或者僅將php包含在索引頁面內,並且如果用戶填寫了所有表格,則發送郵件,否則在未填寫的字段周圍顯示錯誤。

使用jQuery的示例: 檢查所有輸入字段是否已用jQuery填寫

您應該首先檢查並驗證平台,如果平台提交正確,則只有u可以使用header()重定向用戶。

ps:您的要求實際上不清楚。

希望它可以幫助謝謝!

暫無
暫無

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

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