![](/img/trans.png)
[英]To display a popup for specific time and then redirect to another page using php
[英]Modify php mail script to display a popup message and redirect to another page on the website
我有一個表單,在提交時會使用內置的php mail()
函數發送電子郵件。
我怎么做:
要么
我希望選擇1。
if($_POST) {
$name = trim(stripslashes($_POST['contactName']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Check Name
if (strlen($name) < 2) {
$error['name'] = "Please enter your name.";
}
// Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
$error['message'] = "Please enter your message. It should have at least 15 characters.";
}
// Subject
if ($subject == '') { $subject = "Contact Form Submission"; }
// Set Message
$message .= "Email from: " . $name . "<br />";
$message .= "Email address: " . $email . "<br />";
$message .= "Message: <br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
// Set From: header
$from = $name . " <" . $email . ">";
// Email Headers
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (!$error) {
ini_set("sendmail_from", $siteOwnersEmail); // for windows server
$mail = mail($siteOwnersEmail, $subject, $message, $headers);
if ($mail) { echo "ok. (Here pop up saying its submitted and redirects)"; }
else { echo "Something went wrong. Please try again."; }
} # end if - no validation error
else {
$response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
$response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
echo $response;
} # end if - there was a validation error
}
?>
提交表單時,您將啟動一個新請求,以便PHP可以輸出您喜歡的任何html / javascript; 您也可以告訴它重定向到新頁面。
因此,您可以輕松實現任何一個選項,以顯示彈出窗口,只需輸出相關的javascript。
如果您是指彈出一個簡單的警報對話框,則您需要輸出的javascript是alert('Email sent successfully')
-如果您指的是打開一個新窗口,則它將是window.open('newpage.html')
無論哪種方式,您都需要將命令包裝在腳本標簽中:
echo "<script>alert('Email sent successfully');</script>";
這應該放在您的<head>
或<body>
標記中。
當您說重定向時,我不確定這是否必要,但這取決於您將代碼放置在何處。 發送電子郵件后,您可以選擇輸出完全不同的HTML,從而創建新頁面。 您需要確保在您的舊頁面進行任何輸出之前,您的電子郵件代碼已經發生。 例如
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
//process form and send email
$mail = mail($siteOwnersEmail, $subject, $message, $headers);
if ($mail) {
include 'emailsent.php';
}else{
$include 'emailerror.php';
}
}else{
include 'form.php'
}
您可以用一系列回顯替換包含,也可以退出php
if ($mail) {
?><html>...</html><?php
}
如果您確實要重定向,則可以使用標頭函數發送Location標頭:
header('Location: emailsent.html');
有關此信息,請參見php文檔: http : //php.net/manual/zh/function.header.php
如果執行此操作,則必須確保在開始任何輸出(回顯/打印等)之前執行此操作-這還包括PHP之前的任何內容,例如<html><?php
會導致如下錯誤:
警告:無法修改標頭信息-第23行的/some/file.php中已經發送過的標頭(輸出從/some/file.php:12開始)
您可以在此處找到更多信息: 如何修復PHP中的“已發送標頭”錯誤
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.