11

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!

Cyclone
  • 17,939
  • 45
  • 124
  • 193
  • 1
    Several answers below show *how* this can be done. Just remember that the request *itself* is of *one and only one* type, i.e., the `method` of the `
    `. It follows that you can add "`GET`-style" parameters to a `POST` request, but not do the opposite.
    – jensgram Jan 18 '11 at 17:11
  • 2
    I'd really like to know why you have to do this. I can't think of any reason off the top of my head. Also, data being sent via POST is no more secure than being sent by GET. It's sent in the clear either way. If you need security, you must use SSL. – Brad Jan 18 '11 at 17:12
  • @Brad For some reason it only works when it's `POST` for chrome and ff users, and only works when it's `GET` for IE users. – Cyclone Jan 18 '11 at 17:19
  • 1
    What does? Can you give us some HTML to look at? We have no idea what you are referring to. POST and GET are very basic, and should work on any browser. If it isn't, I suspect your problem is something else. – Brad Jan 18 '11 at 17:24
  • There's a form embedded in an iframe, specifically a facebook application iframe. I'm not sure why, but POST doesnt work for IE users and GET isnt working for chrome or ff. – Cyclone Jan 18 '11 at 18:01
  • It would probably be sensible if you provided more information about the root problem. Maybe show some code. – Pekka Jan 18 '11 at 18:02

5 Answers5

32

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.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • 1
    You misunderstand me. I mean I want the data to be submitted as both `POST` and `GET` at once, not pass additional GET `data`. – Cyclone Jan 18 '11 at 18:00
  • 1
    @Cyclone ah, I see. Then this is possible only using JS like @casablanca shows. – Pekka Jan 18 '11 at 18:01
  • Dont forget to set the method to POST. If you will leave it as GET the parameters in the action will be dropped... – Rubberducker Jul 25 '14 at 15:29
12

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('&');
}
casablanca
  • 69,683
  • 7
  • 133
  • 150
4

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()">
webbiedave
  • 48,414
  • 8
  • 88
  • 101
  • 1
    The only thing I would add is .preventDefault() to the event. then call click() of the form submit button after. That way the form can't be send until js has done its thing. – Robert Hurst Jan 18 '11 at 18:53
  • @Robert: Tweaks can be definitely be made. I just wanted to show the general concept in using jQuery for this purpose. – webbiedave Jan 18 '11 at 19:17
2

the form should set post do the get in the url

<form method="post" action="http://www.yourpage.php?firstparam=1&sec=2">
.
.
</form>
yossi
  • 12,945
  • 28
  • 84
  • 110
0

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>