为什么不';t我的提交按钮重定向到一个新页面(使用PHP)


Why doesn't my submit button redirect to a new page (using PHP)

单击"确认"按钮时,我正试图将页面重定向到$PaymentPage。出于某种原因,当我点击它时,什么都不会发生。

我已经发布了表单方法,我已经检查过,如果提交,直接到$PaymentPage。

       <form method="post"></form> 
        <div class="col-sm-offset-2 col-sm-4">
            <button type="submit" class="btn btn-default">Confirm</button>
        </div>
        </form>
        <?php
        if ( isset( $_POST['submit'] ) ) {
            header("Location: $PaymentPage", false);
        }
        ?>

这里有一些问题。首先,提交按钮没有与条件语句匹配的name属性,所以这种情况永远不会发生。

另外,在PHP的顶部使用HTML在头之前输出。

然后,在<form method="post">之后的额外的</form>标签;需要删除。

因此,您需要将代码修改为:

<?php
if ( isset( $_POST['submit'] ) ) {
    header("Location: $PaymentPage", false);
    exit; // added to stop execution if more code is below it
}
else{
   echo "It is not set.";
  }
?>
<form method="post">
<div class="col-sm-offset-2 col-sm-4">
    <button name="submit" type="submit" class="btn btn-default">Confirm</button>
</div>
</form>

错误报告会向您抛出未定义的索引提交通知。

错误报告添加到文件顶部,这将有助于查找错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code

旁注:显示错误只能在暂存中进行,而不能在生产中进行。

注意:

现在,$PaymentPage是未定义的,所以我不知道你在哪里定义了它,也不知道这个值应该是什么

旁注:当没有定义对另一个文件的操作时,表单默认为"self"。


参考文献:

  • 如何修复";标头已发送";PHP中的错误
  • http://php.net/manual/en/function.error-reporting.php
  • http://php.net/manual/en/tutorial.forms.php

它应该会给您一个错误。您可以启用error_reporting,在任何输出之前都需要标头,因此将其移动到文件的顶部。

你也关闭了表单,这是你的按钮什么都不做的原因

<form method="post"></form> 

在其他一些嵌套问题上过早关闭了表单元素,还将变量附加到字符串中,如下所示:

"String". $variable ." more string";

HTML:

<form method="post">
    <div class="col-sm-offset-2 col-sm-4">
        <input type="submit" name="button" class="btn btn-default" value="Confirm">
    </div>
    </form>

PHP:

    <?php
    $page = "http://www.google.com/";
    if ( isset( $_POST['button'] ) ) {
        header("Location: ". $page,false);
    }
    ?>