PHP联系表单重定向

问题描述 投票:0回答:2

我有一个PHP联系表单,当用户填写表单并点击发送按钮时,表单将被重定向到主页。我想在重定向时在主页/索引页面上发送成功/失败消息。我怎么能这样做?

我有一个想法是在会话中存储成功/失败消息然后在主页上调用它但是长时间触摸php因此需要有关如何实现相同的帮助。任何想法和想法将不胜感激。

表单代码(它在与主页不同的文件中):

<?php if (isset($_POST['email'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to      = "[email protected]";
$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); } ?>
php html
2个回答
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”则返回错误消息。


1
投票

要使用PHP实现重定向,您必须使用标头功能。这将重定向到您需要加载的页面。否则,您可以使用表单操作在表单提交后更改获取重定向。

您可以使用header()函数发送新的HTTP标头,但必须在任何HTML或文本之前将其发送到浏览器(例如,在声明之前)。

header('Location: '.$URL);

在这里,您可以根据状态传递带标题的参数。例如,当您调用header时,您可以传递一个参数来显示消息。传递一个变量并根据变量显示相应的消息。

在您的情况下,在页眉中发送电子邮件后,会显示来自会话和重置会话的消息,或者传递具有状态的变量并检查消息的状态,在UI中显示相应的消息

PHP Header Reference

如果您在生产环境中使用它,那么发送这样的电子邮件并不是一个好习惯。它可能将您的域名列入黑名单。使用任何SMTP凭据和邮件程序类来执行此操作

© www.soinside.com 2019 - 2024. All rights reserved.