我有一个PHP联系人表单,当用户填写表单并单击“发送”按钮时,该表单将重定向到主页。我想在重定向时在主页/索引页面上发送成功/失败消息。我该怎么做?
我有一个想法,可以在会话中存储成功/失败消息,然后在主页上调用它,但是长时间触摸php会因此需要帮助来实现这一点。任何想法都将不胜感激。
表单代码(与首页位于不同的文件中):
<?php if (isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "admin@hillierroaddentalclinic.com.au";
$email_subject = "Enquiry from Hiller Road Dental Clinic Website";
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['name']) || !isset($_POST['email']) || !isset($_POST['phone']) || !isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email = $_POST['email']; // required
$phone = $_POST['phone']; // not required
$message = $_POST['message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if (!preg_match($email_exp, $email)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if (!preg_match($string_exp, $name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if (strlen($message) < 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 .= "Name: " . clean_string($name) . "\n";
$email_message .= "Email: " . clean_string($email) . "\n";
$email_message .= "Phone: " . clean_string($phone) . "\n";
$email_message .= "Message: " . clean_string($message) . "\n";
// create email headers
$headers = 'From: ' . $email . "\r\n" . 'Reply-To: ' . $email . "\r\n" . 'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
function Redirect($url, $permanent = false)
{
header('Location: ' . $url, true, $permanent ? 301 : 302);
exit();
}
Redirect('http://hillierroaddentalclinic.com.au/', false); } ?>
答案 0 :(得分:1)
要使用PHP实现重定向,您必须使用标头功能。这将重定向到您需要加载的页面。否则,您可以在提交表单后使用表单操作来更改获取重定向。
您可以使用header()函数发送新的HTTP标头,但是必须在任何HTML或文本之前(例如,在声明之前)将其发送到浏览器。
header('Location: '.$URL);
在这里,您可以根据状态传递带有标题的参数。例如,当您调用标题时,可以传递用于显示消息的参数。传递一个变量,并根据该变量显示适当的消息。
在您发送电子邮件后,您的页面标题中的页面会显示会话中的消息并重新设置会话,或者传递带有状态的变量并检查消息的状态以在UI中显示适当的消息
如果在生产环境中使用电子邮件,则发送此类电子邮件不是一个好习惯。可能会将您的域列入黑名单。使用任何SMTP凭据和邮件程序类
答案 1 :(得分:1)
来自PHP的联系方式
if(mail($email_to, $email_subject, $email_message, $headers )){
header("location: index.php?status=1"); //success
}else{
header("location: index.php?status=0"); //failure
}
在index.php中
if(isset($_GET['status'])){
$status = $_GET['status'];
if($status == 1){
echo "Thank you for your message";
}else if($status == 0){
echo "Unable to send message";
}
}
从index.php中的URL获取状态值,如果值为“ 1”,则显示成功消息,如果状态为“ 0”,则显示错误消息。