php 设置 $_POST 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17264103/
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
Setting $_POST variables
提问by Cody Thompson
I have a form using POST, and a variable. How would I set that variable in $_POST so that after the form is submitted, I can still get the variable?
我有一个使用 POST 的表单和一个变量。我如何在 $_POST 中设置该变量,以便在提交表单后,我仍然可以获得该变量?
I tried just
我试过了
$_POST['variable'] = $variable;
It ends up empty.
它最终是空的。
回答by Jerska
You should either put that variable as an hidden field in your form, or use a session variable.
您应该将该变量作为隐藏字段放入表单中,或者使用会话变量。
Hidden field
隐藏字段
<form method="POST" action="someactionpage.php">
<input type="hidden" name="my_var" value="<?php echo $myvar; ?>" />
<!-- ... -->
</form>
And get it after in someactionpage.phpwith $_POST['my_var']
when the form is submitted.
而在之后得到它someactionpage.php与$_POST['my_var']
时提交表单。
Session variable
会话变量
Just store it whithin the $_SESSION variable
只需将其存储在 $_SESSION 变量中
<?php
session_start (); // Just once at the beginning of your code
// ...
$_SESSION['my_var'] = $myvar;
?>
and retrieve it on another page with
并在另一个页面上检索它
<?php
session_start (); // Same than before
// ...
echo $_SESSION['my_var'];
?>
Additional info
附加信息
As pointed out in some answers and comments, you should always check that the variable is present, because you have no guarantee of that. Just use the isset function
正如一些答案和评论中所指出的,您应该始终检查变量是否存在,因为您无法保证这一点。只需使用isset函数
if (isset ($_SESSION['my_var']))
// Do stuff with $_SESSION['my_var']
or
或者
if (isset ($_POST['my_var']))
// Do stuff with $_POST['my_var']
As pointed out by Kolinkin the comments, a field value (sended via POST) can be easily seen and changed by the user. So always prefer session variables unless it is really non-critical info.
正如Kolink在评论中指出的那样,用户可以轻松查看和更改字段值(通过 POST 发送)。所以总是更喜欢会话变量,除非它真的是非关键信息。
回答by MrBoolean
PHP is a server side language! You cannot set a variable and use it on another instance. That means, that PHP resets all the stuff after you process an reload. To setup a variable which is defined after a reload you have to use the current session. See: http://de1.php.net/manual/en/book.session.php
PHP 是一种服务器端语言!您不能设置变量并在另一个实例上使用它。这意味着,PHP 会在您处理重新加载后重置所有内容。要设置在重新加载后定义的变量,您必须使用当前会话。见:http: //de1.php.net/manual/en/book.session.php
<?php
session_start();
$_SESSION['variable'] = 'my content';
回答by CompuSolver
You can set $_POST['my_var'] (any name) either before or after submission:
您可以在提交之前或之后设置 $_POST['my_var'] (任何名称):
<input type="text" name="my_var" value="<?= $myvar ?>" />
(Note, field type does not have to be 'hidden' as posted earlier) If, say, you have code that processes the form from two different types of screens and must change one of the form vars after submission, merely do this in the post-submission code:
(注意,字段类型不必像之前发布的那样“隐藏”)如果,例如,您有处理来自两种不同类型屏幕的表单的代码,并且必须在提交后更改其中一个表单变量,只需在提交后代码:
$_POST['t_mytext'] = "changed value (or whatever)";
回答by Dany Caissy
$_POST variables are sent from the previous page to the page you are currently in.
$_POST 变量从上一页发送到您当前所在的页面。
That means you shouldn't actually set a POST variable, you should only retrieve its content.
这意味着您实际上不应该设置 POST 变量,您应该只检索其内容。
If you want to set a post variable for the next time you submit a form, you can do this:
如果你想在下次提交表单时设置一个 post 变量,你可以这样做:
<input type="hidden" name="variable" value="<?php echo $_POST['variable']" />
This means the page you are submitting to will be able to access your variable this way:
这意味着您提交的页面将能够以这种方式访问您的变量:
$variable = $_POST['variable'];
If you want a variable to stay with a certain user, you should look into sessions.
如果您希望变量保留给某个用户,您应该查看会话。
回答by Boundless
You can use $_POST['variable'] = $foo to set a post variable. The $_POST['variable'] will continue to be set until the php script has finished executing, or until you use: unset($_POST['variable']). If you are trying to keep the variable around between sessions (which it sounds like), you should use a session.
您可以使用 $_POST['variable'] = $foo 来设置 post 变量。$_POST['variable'] 将继续设置,直到 php 脚本完成执行,或者直到您使用:unset($_POST['variable'])。如果您试图在会话之间保留变量(听起来像),则应该使用会话。
in session 1:
在第 1 节中:
session_start();
$_SESSION['variable'] = 'foo';
to retrieve in another call to the server:
在对服务器的另一个调用中检索:
session_start();
$variable = $_SESSION['variable'];
回答by Thomas Kos
Try this:
尝试这个:
$variable = isset($_POST['variable']) ? $_POST['variable'] : '';