提交表单后无法重定向

Cannot redirect after form submission

本文关键字:重定向 表单 提交      更新时间:2023-09-26

我在HTML页面上有一个包含表单数据的表单,并希望在用户按下提交按钮并通过电子邮件发送信息后将其重定向到新的URL页面。使用以下按钮调用窗体。

<div class="btnp"><input type="submit"  value="Continue to Billing" ></div>

然后表单通过邮寄将数据发送到我的 PHP 文件。我不需要成功的回显消息,我只想将URL重定向到付款页面。下面是我设置的 PHP 文件。我正在尝试提交,发送带有表单日期的电子邮件,然后重定向到新的 html URL。

<?php
//Retrieve form data. 
//POST
$URL = "http://www.google.com"; 
$fname  = $_POST['fname'];              
$zip = $_POST['zip'];       
$phone = $_POST['phone'];       
$email = $_POST['email'];       
    //recipient - change this to your name and email
    $to = 'sales@mysite.com';   
    //sender
    $from = $fname . ' <' . $email . '>';
    //subject and the html message
    $subject = 'New User: ' . $fname;   
    $message = '
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head></head>
    <body>
        <table>
            <tr><td>First Name</td><td>' . $fame . '</td></tr>      
            <tr><td>Zip</td><td>' . $zip . '</td></tr>      
            <tr><td>Telephone</td><td>' . $phone . '</td></tr>  
            <tr><td>Email</td><td>' . $email . '</td></tr>  
        </table>
    </body>
    </html>';
    //send the mail
    $result = sendmail($to, $subject, $message, $from);
    if ($_POST) 
        if ($result) header('Location: '.$URL);
        else echo 'Sorry, unexpected error. Please try again later';
    //Simple mail function with HTML header
    function sendmail($to, $subject, $message, $from) {
    $headers = "MIME-Version: 1.0" . "'r'n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "'r'n";
    $headers .= 'From: ' . $from . "'r'n";  
    $result = mail($to,$subject,$message,$headers);
    }       
?>    

要使用"header"函数:

header("location: result.php");

您无法将任何内容输出/打印/回显到屏幕上,因此请尝试以下操作:

if ($_POST) 
        if ($result) header("location: result-page.php");
        else echo 'Sorry, unexpected error. Please try again later';

或者你可以使用javascript打印成功的消息并重定向:

if ($_POST) 
        if ($result) { 
            print "<script>alert('Ok, email sent');</script>";
            print "<script>window.open('result-page.php','_self');</script>";
        }else{
            print 'Sorry, unexpected error. Please try again later';
        }

使用您尝试过的header('Location: $url);代码段,但将函数更正为:

header('Location: '.$URL);

只需确保定义了变种$URL即可。