views:

236

answers:

4

Is there a well-supported, common behavior that I can expect if I do something like this in HTML:

<form method="get" action="/somePage.html?param1=foo&param2=foo">
  <input name="param2"></input>
  <input name="param3"></input>
</form>

Seems like this sort of thing is inherently ridiculous, but I've seen it used here and there and I was wondering what on Earth the expected behavior should be. Are browsers smart enough to tack on "&param2=whatever&param3=whatever" to the action, or do they just throw in a second question mark? Or what? Are there cases where this is actually the right way to do things?

+7  A: 

If the method attribute is set to GET, the browser drops the querystring parameters from the action attribute before constructing the form argument values.

So in your example, the request to the server on submit will look like: /somePage.html?param2=value&param3=value

So no, when the method is "GET", as in your example, there's no reason to do this.

Rex M
thomasrutter
@thomasrutter quite right! I think that's slightly outside the scope of this discussion. The interesting part is the obvious collision.
Rex M
@Rex - I think it's fascinating that the behavior is different for GET and POST. That I did not expect. Is there a logical reason why?
CaptainAwesomePants
@CaptainAwesomePants - by definition, using "GET" requires use of the querystring. The easiest way to handle it risk-free is to blow away anything first. POST has no need for the querystring, so no need to remove it to make room.
Rex M
Ahh, good point.
CaptainAwesomePants
+1  A: 

Not sure but I think it's better practice to place those variables in hidden input fields. This it doesn't matter if your posting method is either POST or GET.

<form method="get" action="/somePage.html">
  <input name="param2"></input>
  <input name="param3"></input>
  <input type="hidden" name="param1" value="foo" />
  <input type="hidden" name="param2" value="foo" />
</form>
Luke
+1  A: 

You could change the method attribute in the form to "POST" with script before posting the form, so there could be a use for the query string in the action. It hardly seems to be the best solution for anything, though.

Guffa
+1  A: 

Well, all the questions have been answered except the last, to which the answer is yes. For POST, it's allowed, but you may well find cases where it doesn't work. I've seen web servers that only allow postdata or querystring, so it's not dependable.

Mark