dongrao9436 2013-07-25 13:33
浏览 10
已采纳

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

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. 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 
Email: $email 
Telephone: $tel 

Details: $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>
  • 写回答

6条回答 默认 最新

  • doujiang9887 2013-07-25 13:37
    关注

    The problem is you're outputting data before calling the header(), it won't work like this.

    Put all the PHP above the form.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 解riccati方程组