在条件之后重定向到另一个页面


Redirect to another page after if condition

从表单提交数据后,我想被重定向到一个页面,下面是我的代码:

<form action="#result" method="POST">
  <input name="zipcode" type="text" placeholder="Your ZipCode" />
  <input name="zipcode_submit" type="submit" value="Send" />
</form>
<div id="result">
<?php 
    if(isset($_POST['zipcode_submit'])) {
        header("Location: http://twitter.com");
    }
?>
</div>

它不适合我,我不知道为什么

谢谢你的帮助

尝试将PHP代码移到表单标签上方,即

 <?php 
    if(isset($_POST['zipcode_submit'])) {
        header("Location: http://twitter.com");
    }
   ?>
上面的

<form action="#result" method="POST">

查看php.net文档中有关header函数的内容。它必须是网站的第一个输出。把你的表单放在文件的末尾。

  <html>
<?php
    /* this will produce an error, header must be the first output on the website */
    header('Location: http://www.example.com/');
    exit;
    ?>

你应该设置一个出口;在头语句之后,完全防止以下代码被执行