Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send both POST and GET in a form

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!

like image 492
Cyclone Avatar asked Jan 18 '11 17:01

Cyclone


People also ask

How can I use GET and POST in same form?

You cannot send the values of some input fields via GET and others via POST with PHP and HTML. If you add JavaScript, you could add the values of some input fields to the query string of the action-attribute of the form.

Can I use GET and POST at the same time?

With Ajax you can send data to a PHP script via GET and POST in the same request. The process is similar to sending data through the POST, it's actually the same, being only applied a "trick". - the values to be sent with POST method are added in the parameter of the "send()" function.

How can use both GET and POST method in HTML?

The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute). The form-data can be sent as URL variables (with method="get" ) or as HTTP post transaction (with method="post" ). Notes on GET: Appends form-data into the URL in name/value pairs.

Are GET and POST identical?

If you are working RESTfully, GET should be used for requests where you are only getting data, and POST should be used for requests where you are making something happen.


1 Answers

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('&');
}
like image 136
casablanca Avatar answered Oct 23 '22 05:10

casablanca