简体   繁体   English

提交表单后重定向到另一个页面?

[英]Redirect to another page after submitting a form?

I'm trying to redirect users to another page after a form has been submitted. 提交表单后,我试图将用户重定向到另一个页面。 The form works great. 该表格效果很好。 If forces users to input the required fields, and is not submitted unless each of these fields are complete. 如果强制用户输入必填字段,则除非这些字段中的每一个均完整,否则不会提交。 It then submits the form and refreshes. 然后,提交表单并刷新。 But I would like to redirect users to another page instead of simply refreshing the page. 但是我想将用户重定向到另一个页面,而不是简单地刷新页面。

My php code is below! 我的PHP代码如下!

<?php

$recipients = 'dre@myemail.com';
//$recipients = '#';

try {
require './phpmailer/PHPMailerAutoload.php';

preg_match_all("/([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]   {2,6}(?:\.[a-z]{2})?)/", $recipients, $addresses, PREG_OFFSET_CAPTURE);

if (!count($addresses[0])) {
    die('MF001');
}

if (preg_match('/^(127\.|192\.168\.)/', $_SERVER['REMOTE_ADDR'])) {
    die('MF002');
}

$template = file_get_contents('rd-mailform1.tpl');

if (isset($_POST['form-type'])) {
    switch ($_POST['form-type']){
        case 'registration':
            $subject = 'New Registration';
            break;
        case 'subscribe':
            $subject = 'Subscribe request';
            break;
        case 'order':
            $subject = 'Order request';
            break;
        default:
            $subject = 'A message from your site visitor';
            break;
    }
}else{
    die('MF004');
}

if (isset($_POST['email'])) {
    $template = str_replace(
        ["<!-- #{FromState} -->", "<!-- #{FromEmail} -->"],
        ["Email:", $_POST['email']],
        $template);
}else{
    die('MF003');
}

if (isset($_POST['message'])) {
    $template = str_replace(
        ["<!-- #{MessageState} -->", "<!-- #{MessageDescription} -->"],
        ["Message:", $_POST['message']],
        $template);
}

preg_match("/(<!-- #{BeginInfo} -->)(.|\n)+(<!-- #{EndInfo} -->)/", $template, $tmp, PREG_OFFSET_CAPTURE);
foreach ($_POST as $key => $value) {
    if ($key != "email" && $key != "message" && $key != "form-type" && !empty($value)){
        $info = str_replace(
            ["<!-- #{BeginInfo} -->", "<!-- #{InfoState} -->", "<!-- #{InfoDescription} -->"],
            ["", ucfirst($key) . ':', $value],
            $tmp[0][0]);

        $template = str_replace("<!-- #{EndInfo} -->", $info, $template);
    }
}

$template = str_replace(
    ["<!-- #{Subject} -->", "<!-- #{SiteName} -->"],
    [$subject, $_SERVER['SERVER_NAME']],
    $template);

$mail = new PHPMailer();
$mail->From = $_SERVER['SERVER_ADDR'];
$mail->FromName = $_SERVER['SERVER_NAME'];

foreach ($addresses[0] as $key => $value) {
    $mail->addAddress($value[0]);
}

$mail->CharSet = 'utf-8';
$mail->Subject = $subject;
$mail->MsgHTML($template);

if (isset($_FILES['attachment'])) {
    foreach ($_FILES['attachment']['error'] as $key => $error) {
        if ($error == UPLOAD_ERR_OK) {
            $mail->AddAttachment($_FILES['attachment']['tmp_name'][$key],    $_FILES['Attachment']['name'][$key]);
        }
    }
}

$mail->send();

die('MF000');
} catch (phpmailerException $e) {
die('MF254');
} catch (Exception $e) {
die('MF255');
}

?>

You could use header to redirect the page. 您可以使用标题重定向页面。 Where ever you determine that the form's action was successfully executed, add this: 在确定表单操作已成功执行的任何地方,添加以下内容:

header('Location: path/to/your/file.php');  //Could also be an external or internal URL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM