如何使我的PHP表单重定向工作?

如何使我的PHP表单重定向工作?,php,html,forms,Php,Html,Forms,我已经用下面的html和php代码创建了一个表单,基本上它会运行一个检查,看看垃圾邮件问题是否已经成功完成。在这种情况下,它应该处理表单,发送电子邮件,并将用户重定向到thankyou.html页面。然而,目前,一旦成功,表单只会重新加载同一页面,而不会重定向到感谢页面,但我确实收到了一封确认电子邮件 在下面的代码中是否有我遗漏/出错的东西来阻止此操作 <form id="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" me

我已经用下面的html和php代码创建了一个表单,基本上它会运行一个检查,看看垃圾邮件问题是否已经成功完成。在这种情况下,它应该处理表单,发送电子邮件,并将用户重定向到thankyou.html页面。然而,目前,一旦成功,表单只会重新加载同一页面,而不会重定向到感谢页面,但我确实收到了一封确认电子邮件

在下面的代码中是否有我遗漏/出错的东西来阻止此操作

<form id="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <table>
        <tr>
            <td align="right"><label for="name">Name:<span class="error"><?php echo $nameErr;?></span></label></td>
            <td><input type="text" name="name" id="name" /></td>
        </tr>
        <tr>
            <td align="right"><label for="email">Email:<span class="error"><?php echo $emailErr;?></span></label></td>
            <td><input type="text" name="email" id="email"  /></td>
        </tr>
        <tr>
            <td align="right"><label for="telephone">Telephone:<span class="error"><?php echo $telErr;?></span></label></td>
            <td><input name="telephone" type="text" id="telephone" /></td>
        </tr>
        <tr>
            <td align="right"><label for="field">Anti-spam question:<span class="error"><?php echo $spamErr;?></span></label></td>
            <td><input name="field" type="text" id="field" value="Complete the Beatles lyric: all you need is...?"  onfocus="if(this.value==this.defaultValue) this.value='';"  onblur="if(this.value == ''){this.value='Complete the Beatles lyric: all you need is...?';}" /></td>
        </tr>
        <tr>
            <td align="right"><label for="message">Message:</label></td>
            <td><textarea name="message"></textarea></td>
        </tr>
        <tr>
            <td colspan="2" align="right"><img src="images/contactdots.gif" width="338" height="10" alt="" /></td>
        </tr>
        <tr>
            <td colspan="2" align="right"><input id="button" type="Submit" value="SUBMIT" alt="submit" /></td>
        </tr>
    </table>
<?php

if($_SERVER['REQUEST_METHOD'] == "POST"){

// Pick up the form data and assign it to variables
$name = stripslashes($_POST['name']);
$email = stripslashes($_POST['email']);
$tel = $_POST['telephone'];
$comments = stripslashes($_POST['message']);
$field = strtolower($_POST['field']);
$spam_check = 'love';
if($field == $spam_check){
// Build the email (replace the address in the $to section with your own)
$to = 'my@email.com';
$subject = "The Vintage Affair Web Quote enquiry";
$comments = "Name: $name \nEmail: $email \nTelephone: $tel \n\nDetails: $comments";
$headers = "From: my@email.com" . PHP_EOL . "Reply-To: my@email.com";

// Send the mail using PHPs mail() function
mail($to, $subject, $comments, $headers);

// Redirect
header("Location: thankyou.html");
}
else{ echo '<div class="spam">*You got the Beatle\'s lyric wrong, please try again*</div>'; }
}

?>
</form>
试试这个,也许:

header("Location: thankyou.html");
exit();
试试这个,也许:

header("Location: thankyou.html");
exit();

问题是在调用header()之前要输出数据,它不会这样工作


将所有PHP放在表单上方。

问题是在调用header()之前输出数据,它不会这样工作

将所有PHP放在表单上方。

header()
必须在任何输出之前发送,如果输出是空格或字符串

您必须在提交前检查表单,如下所示:

<?php

if( isset($_POST['myform'] )
{
 if( spam_check($_POST['myform']) === true )
 {
   header("Location: thankyou.html");
 }
 else 
 { 
  echo '<div class="spam">*You got the Beatle\'s lyric wrong, please try again*</div>';
 }
}
else
{
  display_form();
}
?>
header()
必须在任何输出之前发送,如果输出是空格或字符串

您必须在提交前检查表单,如下所示:

<?php

if( isset($_POST['myform'] )
{
 if( spam_check($_POST['myform']) === true )
 {
   header("Location: thankyou.html");
 }
 else 
 { 
  echo '<div class="spam">*You got the Beatle\'s lyric wrong, please try again*</div>';
 }
}
else
{
  display_form();
}
?>

好的,那么您的问题是,在运行检查之前,您实际上在屏幕上输出了内容。
在您的案例中,HTML代码的任何输出之前都会发送标题

使用ob_start()ob_end_flush()在文件开头配对,如

<?php 
   ob_start(); // this stores the output in a buffer for later use
?>

html code here

<?php



if($_SERVER['REQUEST_METHOD'] == "POST"){

    // Pick up the form data and assign it to variables
    $name = stripslashes($_POST['name']);
    $email = stripslashes($_POST['email']);
    $tel = $_POST['telephone'];
    $comments = stripslashes($_POST['message']);
    $field = strtolower($_POST['field']);
    $spam_check = 'love';

    if($field == $spam_check){
        // Build the email (replace the address in the $to section with your own)
        $to = 'my@email.com';
        $subject = "The Vintage Affair Web Quote enquiry";
        $comments = "Name: $name \nEmail: $email \nTelephone: $tel \n\nDetails: $comments";
        $headers = "From: my@email.com" . PHP_EOL . "Reply-To: my@email.com";

        // Send the mail using PHPs mail() function
        mail($to, $subject, $comments, $headers);

        // Redirect
        header("Location: thankyou.html");
   }
    else echo '<div class="spam">*You got the Beatle\'s lyric wrong, please try again*</div>';
}

ob_end_flush(); // this is for printing the buffered output
?>

这里是html代码

ob_end_flush()最后

好的,那么您的问题是,在运行检查之前,您实际上在屏幕上输出了内容。 在您的案例中,HTML代码的任何输出之前都会发送标题

使用ob_start()ob_end_flush()在文件开头配对,如

<?php 
   ob_start(); // this stores the output in a buffer for later use
?>

html code here

<?php



if($_SERVER['REQUEST_METHOD'] == "POST"){

    // Pick up the form data and assign it to variables
    $name = stripslashes($_POST['name']);
    $email = stripslashes($_POST['email']);
    $tel = $_POST['telephone'];
    $comments = stripslashes($_POST['message']);
    $field = strtolower($_POST['field']);
    $spam_check = 'love';

    if($field == $spam_check){
        // Build the email (replace the address in the $to section with your own)
        $to = 'my@email.com';
        $subject = "The Vintage Affair Web Quote enquiry";
        $comments = "Name: $name \nEmail: $email \nTelephone: $tel \n\nDetails: $comments";
        $headers = "From: my@email.com" . PHP_EOL . "Reply-To: my@email.com";

        // Send the mail using PHPs mail() function
        mail($to, $subject, $comments, $headers);

        // Redirect
        header("Location: thankyou.html");
   }
    else echo '<div class="spam">*You got the Beatle\'s lyric wrong, please try again*</div>';
}

ob_end_flush(); // this is for printing the buffered output
?>

这里是html代码

ob_end_flush()最后

我遇到了同样的问题,我找到了这个解决方案:

if (!headers_sent()){
    header('Location: thankyou.html');
}
else {
    echo '<script>';
    echo 'window.location.href="thankyou.html";';
    echo '</script>';
    echo '<noscript>';
    echo '<meta http-equiv="refresh" content="0; url=thankyou.html" />';
    echo '</noscript>';
}
if(!headers\u sent()){
标题('Location:thankyou.html');
}
否则{
回声';
echo'window.location.href=“thankyou.html”;
回声';
回声';
回声';
回声';
}

我希望它对您有用

我遇到了同样的问题,我找到了这个解决方案:

if (!headers_sent()){
    header('Location: thankyou.html');
}
else {
    echo '<script>';
    echo 'window.location.href="thankyou.html";';
    echo '</script>';
    echo '<noscript>';
    echo '<meta http-equiv="refresh" content="0; url=thankyou.html" />';
    echo '</noscript>';
}
if(!headers\u sent()){
标题('Location:thankyou.html');
}
否则{
回声';
echo'window.location.href=“thankyou.html”;
回声';
回声';
回声';
回声';
}

我希望它对您有用

将表单放在php代码下面

将表单放在php代码下面

您必须在任何输出之前放置
header()
调用。或者使用输出缓冲。您必须将
header()
调用放在任何输出之前。或者使用输出缓冲。太好了……这很有效!也感谢您的快速响应。没问题,为了将来参考,有时在头()之前不可避免地要有输出,在这种情况下,您可以使用ob_start()/ob_end_flush()来缓冲您的输出,您可以在此处阅读有关输出控制的更多信息:非常好……这很有效!也感谢您的快速响应。没问题,为了便于将来参考,有时在页眉()之前不可避免地要有输出,在这种情况下,您可以使用ob_start()/ob_end_flush()缓冲您的输出,您可以在此处阅读有关输出控制的更多信息: