简体   繁体   English

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

[英]How do I get my PHP form redirect to work?

I've created a form with the following html and php code, and basically it runs a check to see if the spam question has been completed successfully in which case it should process the form, send an email through and redirect the user to the thankyou.html page. 我创建了一个包含以下html和php代码的表单,基本上它运行检查以查看垃圾邮件问题是否已成功完成,在这种情况下它应该处理表单,发送电子邮件并将用户重定向到thankyou .html页面。 However at the moment, on success, the form just reloads the same page and doesn't redirect to the thank you page, however I do receive a confirmation email... 但是,目前,成功时,表单只是重新加载相同的页面,并没有重定向到感谢页面,但是我收到了确认电子邮件...

Is there something I've missed off/got wrong in the code below to stop this from actioning? 在下面的代码中是否有一些我错过/出错的东西来阻止这个行动?

<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>

The problem is you're outputting data before calling the header(), it won't work like this. 问题是你在调用header()之前输出数据,它不会像这样工作。

Put all the PHP above the form. 将所有PHP放在表单上方。

header() must be sent before ANY output, should it be a space or a string. header()必须在ANY输出之前发送,如果它是空格或字符串。

You must check the form submission before, like this: 您必须先检查表单提交,如下所示:

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

Being dispay_form() a function that displays all the HTML, maybe with a file_get_contents() of a HTML file. dispay_form()是一个显示所有HTML的函数,可能是HTML文件的file_get_contents()

I have met the same problem and I've found this solution : 我遇到了同样的问题,我找到了这个解决方案:

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

I hope that it Will be useful for you 我希望它对你有用

Try this maybe: 试试吧:

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

Ok so your problem is that you actually output stuff on the screen before you run your checks. 好吧,问题是你在运行支票之前实际输出了屏幕上的内容。 Headers are sent right before any output in your case the HTML code. 在您输入HTML代码的任何输出之前发送标头。

Use ob_start(); 使用ob_start(); ob_end_flush(); ob_end_flush()函数; pair at the beginning of the file something like 在文件的开头配对的东西

<?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
?>

ob_end_flush(); ob_end_flush()函数; at the end 在末尾

把表格放在php代码下面

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

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