php 在表单中同时发送 POST 和 GET
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4726809/
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
Send both POST and GET in a form
提问by Cyclone
I need to make a form send both POST and GET requests (due to some bugs in IE and iframes), how can you do so?
我需要让一个表单同时发送 POST 和 GET 请求(由于 IE 和 iframes 中的一些错误),你怎么能这样做?
The data being sent is nothing mega secure so it doesn't matter if it can be set via GET, just need to make sure it is set both ways.
发送的数据不是超级安全的,所以它是否可以通过 GET 设置并不重要,只需要确保它是双向设置的。
Thanks for the help!
谢谢您的帮助!
回答by Pekka
Easy: Just specify the GET data in the form URL.
简单:只需在表单 URL 中指定 GET 数据。
<form method="POST" action="form.php?a=1&b=2&c=3">
however, very carefully check how the data is used in the receiving script. Don't use $_REQUEST
- rather parse $_GET
and $_POST
according to your exact needs and in the priority order you need them.
但是,请非常仔细地检查数据在接收脚本中的使用方式。不要使用$_REQUEST
-而解析$_GET
,并$_POST
根据您的具体需求,并在你需要他们的优先顺序。
回答by casablanca
Make the form do a usual POST and use JavaScript to replicate the values in the query string as well:
使表单执行通常的 POST 并使用 JavaScript 复制查询字符串中的值:
HTML:
HTML:
<form id="myform" method="post" action="..." onsubmit="process()">
...
</form>
JavaScript:
JavaScript:
function process() {
var form = document.getElementById('myform');
var elements = form.elements;
var values = [];
for (var i = 0; i < elements.length; i++)
values.push(encodeURIComponent(elements[i].name) + '=' + encodeURIComponent(elements[i].value));
form.action += '?' + values.join('&');
}
回答by webbiedave
Not sure what bug you're trying to get around but you can use jQueryto easily modify the form's action to contain the posted values:
不确定您要解决什么错误,但您可以使用jQuery轻松修改表单的操作以包含发布的值:
script:
脚本:
function setAction() {
$("#myform").attr("action", "/path/to/script/?" + $("#myform").serialize());
}
html:
html:
<form id="myform" action="/path/to/script/" method="post" onsubmit="setAction()">
回答by yossi
the form should set post do the get in the url
表单应该设置 post 在 url 中获取
<form method="post" action="http://www.yourpage.php?firstparam=1&sec=2">
.
.
</form>
回答by Max van Kampen
If you need a dynamicaly created URL. You can use this HTML example. The GET fields are in a seprated Form. Before submit of the POST Form the URL is generated from the GET Form.
如果您需要动态创建的 URL。您可以使用此 HTML 示例。GET 字段采用单独的形式。在提交 POST 表单之前,从 GET 表单生成 URL。
<form id="formGET">
email: <input name="email" value="[email protected]"/>
</form>
<form id="formPOST" method="post" onsubmit="this.action='/api/Account?'+Array.prototype.slice.call(formGET.elements).map(function(val){return val.name + '=' + val.value}).join('&');">
mobile: <input name="mobile" value="9999999999" /><br />
<button>POST</button>
</form>