繁体   English   中英

我怎么能放一个Html<form> PHP 函数中的标记,这会将我重定向到另一个 .php 文件?

[英]How can I put an Html <form> tag inside a PHP function, which would redirect me to another .php file?

我在网上搜索了几个小时,但似乎找不到答案。

我想要做的是在一个 php 函数中放置一个<form>标签,如果满足某个条件,它会将我重定向到另一个 x.php 文件。

听起来很简单:

function myFunction() {
    if ( Some Condition Here ) {
        ?>
             <form action="x.php">
                 <button>submit</button>
             </form>
        <?php
    }
}

显然,在提交表单之前不会发生“重定向”。 重定向是为了响应 HTTP 请求而不是“具有表单”。

您不使用表单重定向,而是使用标题

function redirect(url){
  header("Location: ".url);
  die();
}

所以现在

if(some_condition){
  redirect("anotherFile.php");
}

对于重定向使用以下代码:

header('Location: page.php');
exit;

暂无
暂无

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

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