php 如何使用php中的表单重定向到当前页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2155115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:16:05  来源:igfitidea点击:

How to redirect to the current page with form in php?

phpforms

提问by SUN Jiangong

I tried to redirect to the current page with form in my php application. Now i have met a problem.

我试图在我的 php 应用程序中使用表单重定向到当前页面。现在我遇到了一个问题。

<form name="myform" action="?page=matching" method="GET">

<input id="match_button" type="submit" name="button" value="button" onClick="func_load3()" />

</form>

action="?page=matching"means the current page, because i use the single entry in my php application.

action="?page=matching"表示当前页面,因为我在我的 php 应用程序中使用了单个条目。

With the code upon, When i click the button, it redirects to the homepage.

随着代码,当我点击按钮时,它重定向到主页。

And I tried to use:

我尝试使用:

<form name="myform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET">

but it still doesn't work.

但它仍然不起作用。

So i have to ask for help from you. Do you have any ideas about that? How to fix it? Thanks in advance.

所以我必须向你寻求帮助。你对此有什么想法吗?如何解决?提前致谢。

回答by Sarfraz

leave the action part empty to redirect to current page eg:

将操作部分留空以重定向到当前页面,例如:

<form action="">

or if you want to redirect to some other page, you can do so like this:

或者如果你想重定向到其他页面,你可以这样做:

<form action="somepage.php">  // page must be in the same directory

If you want to append query string then:

如果要附加查询字符串,则:

<form action="somepage.php?var=value&var2=value">

If you want to redirect to a page one directory back then:

如果您想重定向到第一页目录,则:

<form action="../somepage.php?var=value&var2=value">

Two directories:

两个目录:

<form action="../../somepage.php?var=value&var2=value"> and so on

inside a nother folder

在另一个文件夹内

<form action="yourfolder/somepage.php?var=value&var2=value">

回答by Andy

PHP_SELF doesnt include Query string, you have to tag that on the end

PHP_SELF 不包含查询字符串,你必须在最后标记它

<?php
$formAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) { 
    $formAction .= "?" . $_SERVER['QUERY_STRING']; 
}
?>

then set that as your form action:

然后将其设置为您的表单操作:

<form name="myform" action="<?=$formaction;?>" method="GET">

Thou i guess just doing:

你我猜只是在做:

<form name="myform" action="<?=$_SERVER['PHP_SELF']. "?" . $_SERVER['QUERY_STRING'];?>" method="GET">

Might work too, not sure what it will do if the QUERY_STRING is empty thou

也可以工作,如果 QUERY_STRING 为空,不确定它会做什么

回答by Benoit Vidis

<form name="myform" method="GET">
  <input type="hidden" name="page" value="matching" />
  <input id="match_button" type="submit" name="button" value="button" onClick="func_load3()" />
</form>

Alsoy, the JS function func_load needs not to redirect the page to somewhere.

另外,JS 函数 func_load 不需要将页面重定向到某个地方。

回答by erenon

$_SERVER[PHP_SELF]is probably index.php. Have you looked at it? You need:

$_SERVER[PHP_SELF]可能是 index.php。你看过吗?你需要:

<?php echo $_SERVER['PHP_SELF']. '?page=matching'; ?>

I recommend to build a link helper that manages your routes.

我建议构建一个链接助手来管理您的路线。

回答by turbod

try this index.php:

试试这个 index.php:

switch ($_REQUEST['site']){
  case 'somethings':      
     if ($_POST['k_send']){ 
       //somethings
     }
  ...

form.php

表单.php

<form name="send" action="index.php?site=somethings" 
  <input ...
  <input type="submit" name="k_send" ...

回答by turbod

Work only with post method.

仅使用 post 方法。