Send both POST and GET in a form

主宰稳场 提交于 2019-11-27 15:09:36

问题


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?

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.

Thanks for the help!


回答1:


Easy: Just specify the GET data in the form URL.

<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.




回答2:


Make the form do a usual POST and use JavaScript to replicate the values in the query string as well:

HTML:

<form id="myform" method="post" action="..." onsubmit="process()">
  ...
</form>

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('&');
}



回答3:


Not sure what bug you're trying to get around but you can use jQuery to easily modify the form's action to contain the posted values:

script:

function setAction() {
    $("#myform").attr("action", "/path/to/script/?" + $("#myform").serialize());
}

html:

<form id="myform" action="/path/to/script/" method="post" onsubmit="setAction()">



回答4:


the form should set post do the get in the url

<form method="post" action="http://www.yourpage.php?firstparam=1&sec=2">
.
.
</form>



回答5:


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.

<form id="formGET">
    email: <input name="email" value="email@domain.nl"/>
</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>


来源:https://stackoverflow.com/questions/4726809/send-both-post-and-get-in-a-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!