问题描述
有没有办法在不使用表单相关字段(没有 type='hidden')并且只使用 PHP 的情况下设置 $_POST['var']
.类似的东西
Is there a way to set a $_POST['var']
without using form related field (no type='hidden') and using only PHP.
Something like
$_POST['name'] = "Denniss";
有没有办法做到这一点?
Is there a way to do this?
有人要求我对此进行详细说明.例如,我有一个页面,上面有一个表单,表单看起来像这样
Someone asked me for some elaboration on this. So for example, I have page with a form on it, The form looks something like this
<form method='post' action='next.php'>
<input type='text' name='text' value='' />
<input type='submit' name='submit' value='Submit'/>
</form>
点击提交按钮后,我想重定向到 next.php.有没有办法将 $_POST['text'] 变量设置为另一个值?我如何使这个持久化,以便当我点击另一个提交按钮(例如)时 $_POST['text'] 将是我在 next.php 上设置的内容,而不使用隐藏字段.
Once the submit button is clicked, I want to get redirected to next.php. Is there a way for me to set the $_POST['text'] variable to another value? How do I make this persistent so that when I click on another submit button (for example) the $_POST['text'] will be what I set on next.php without using a hidden field.
如果还不清楚,请告诉我,感谢您的帮助.
Let me know if this is still not clear and thank you for your help.
推荐答案
是的,只需将其设置为另一个值:
Yes, simply set it to another value:
$_POST['text'] = 'another value';
这将覆盖与数组的 text
键对应的先前值.$_POST
是超全局的关联数组,您可以像普通 PHP 数组一样更改值.
This will override the previous value corresponding to text
key of the array. The $_POST
is superglobal associative array and you can change the values like a normal PHP array.
注意:此更改仅在同一 PHP 执行范围内可见.一旦执行完成并加载了页面,$_POST
数组将被清除.新的表单提交将生成一个新的 $_POST
数组.
Caution: This change is only visible within the same PHP execution scope. Once the execution is complete and the page has loaded, the $_POST
array is cleared. A new form submission will generate a new $_POST
array.
如果您想在提交的表单中保留该值,则需要将其作为 input
标记的 value
属性放入表单中或从数据存储中检索它.
If you want to persist the value across form submissions, you will need to put it in the form as an input
tag's value
attribute or retrieve it from a data store.
这篇关于在不使用表单的情况下设置 POST 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!