php 表单 动作 php self

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6862735/
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-26 01:29:31  来源:igfitidea点击:

php form action php self

phpformsaction

提问by Sasindu H

I have a php form like this.

我有一个这样的 php 表单。

<form name="form1" id="mainForm" method="post"enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>">

</form

In form action I want to use page name with parameters. like house.php?p_id=10111. But $_SERVER['PHP_SELF'] gives only the house.php(My page full url is house.php?p_id=10111like this) Please help me to solve this problem. thanks.

在表单操作中,我想使用带有参数的页面名称。喜欢house.php?p_id=10111。但是 $_SERVER['PHP_SELF'] 只给出了house.php(我的页面完整网址是house.php?p_id=10111这样的)请帮我解决这个问题。谢谢。

回答by Shef

How about leaving it empty, what is wrong with that?

让它空着怎么样,那有什么问题?

<form name="form1" id="mainForm" method="post" enctype="multipart/form-data" action="">

</form>

Also, you can omit the action attribute and it will work as expected.

此外,您可以省略 action 属性,它会按预期工作。

回答by shaggy

You can leave action blank or use this code:

您可以将操作留空或使用以下代码:

<form name="form1" id="mainForm" method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['REQUEST_URI'];?>">
</form>

回答by Matt R. Wilson

Leaving the action value blank will cause the form to post back to itself.

将操作值留空将导致表单回传给自身。

回答by joskah

You can use an echo shortcut also instead of typing out "echo blah;" as shown below:

您也可以使用回声快捷方式而不是输入“echo blah;” 如下所示:

<form method="POST" action="<?=($_SERVER['PHP_SELF'])?>">

回答by Lakshman Kambam

This is Perfect. try this one :)

太棒了。试试这个:)

<form name="test" method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>">
/* Html Input Fields */
</form>  

回答by Arda

Another (and in my opinion proper) method is use the __FILE__constant if you don't like to rely on $_SERVERvariables.

__FILE__如果您不喜欢依赖$_SERVER变量,另一种(在我看来是正确的)方法是使用常量。

$parts = explode(DIRECTORY_SEPARATOR, __FILE__);
$fileName = end($parts);
echo $fileName;

About magic and predefined constants: 1, 2.

关于魔法和预定义常量:1, 2

回答by gleb

If you want to be secure use this:<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

如果你想安全使用这个:<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">