php 在不使用表单的情况下设置 POST 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3418044/
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 variable without using form
提问by denniss
Is there a way to set a $_POST['var']without using form related field (no type='hidden') and using only PHP.
Something like
有没有办法在$_POST['var']不使用表单相关字段(没有 type='hidden')而只使用 PHP的情况下设置 a 。就像是
$_POST['name'] = "Denniss";
Is there a way to do this?
有没有办法做到这一点?
EDIT: 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>
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.
单击提交按钮后,我想重定向到 next.php。有没有办法将 $_POST['text'] 变量设置为另一个值?我如何使这个持久化,以便当我点击另一个提交按钮(例如)时 $_POST['text'] 将是我在 next.php 上设置的内容,而不使用隐藏字段。
Let me know if this is still not clear and thank you for your help.
如果这仍然不清楚,请告诉我,并感谢您的帮助。
回答by Sarfraz
Yes, simply set it to another value:
是的,只需将其设置为另一个值:
$_POST['text'] = 'another value';
This will override the previous value corresponding to textkey of the array. The $_POSTis superglobal associative arrayand you can change the values like a normal PHP array.
这将覆盖text与数组键对应的先前值。这$_POST是超全局关联数组,您可以像普通 PHP 数组一样更改值。
Caution:This change is only visible within the same PHP execution scope. Once the execution is complete and the page has loaded, the $_POSTarray is cleared. A new form submission will generate a new $_POSTarray.
注意:此更改仅在同一 PHP 执行范围内可见。一旦执行完成并且页面加载完毕,$_POST数组就会被清空。新的表单提交将生成一个新$_POST数组。
If you want to persist the value across form submissions, you will need to put it in the form as an inputtag's valueattribute or retrieve it from a data store.
如果您希望跨表单提交保留该值,您需要将它作为input标签的value属性放入表单中或从数据存储中检索它。
回答by Rakward
If you want to set $_POST['text'] to another value, why not use:
如果要将 $_POST['text'] 设置为另一个值,为什么不使用:
$_POST['text'] = $var;
on next.php?
在next.php?
回答by bcosca
you can do it using ajax or by sending http headers+content like:
您可以使用 ajax 或通过发送 http headers+content 来做到这一点,例如:
POST /xyz.php HTTP/1.1
Host: www.mysite.com
User-Agent: Mozilla/4.0
Content-Length: 27
Content-Type: application/x-www-form-urlencoded
userid=joe&password=guessme
回答by joan16v
You can do it using jQuery. Example:
您可以使用 jQuery 来完成。例子:
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
$.ajax({
url : "next.php",
type: "POST",
data : "name=Denniss",
success: function(data)
{
//data - response from server
$('#response_div').html(data);
}
});
</script>

