表单提交后无法重定向

时间:2013-11-11 20:14:34

标签: javascript php html forms redirect

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

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

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

<?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);
    }       
?>    

2 个答案:

答案 0 :(得分:2)

使用“标题”功能:

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';
        }

答案 1 :(得分:0)

使用您尝试但正确的功能的header('Location: $url);代码段:

header('Location: '.$URL);

确保定义了变量$URL