2

I've seen questions like this but none really answer this specific question. Can you include get variables in the form tag's action attribute? For example:

<form action="script.php?id=4" method="get">
    <input type="text" name="thing" value="temp">
    <input type="submit">
</form>`

This would theoretically result in the get request: script.php?id=4&thing=temp

I'm aware that you can simply do this:

<form action="script.php" method="get">
    <input type="hidden" name="id" value="4">
    <input type="text" name="thing">
    <input type="submit">
</form>`

But I'm curious as to if the previous method is at all possible.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
terpak
  • 1,131
  • 3
  • 17
  • 35
  • the first method should work fine aswell. – Azrael Jul 15 '14 at 06:59
  • Cool thanks, I'll try it out in my project and let you know! – terpak Jul 15 '14 at 07:02
  • Alright, goodluck! :) – Azrael Jul 15 '14 at 07:04
  • 4
    the first would not work. See this: http://www.w3.org/TR/2011/WD-html5-20110525/association-of-controls-and-forms.html#submit-mutate-action . the query part will be replaced by the form input encoded data. – gp. Jul 15 '14 at 07:06
  • similar post: http://stackoverflow.com/questions/1116019/submitting-a-get-form-with-query-string-params-and-hidden-params-disappear – gp. Jul 15 '14 at 07:12
  • @gp. is right. I'm not sure if this works as Anmol stated with combining GET and POST, but with a form's method being GET, the query is completely replaced. Thank you! – terpak Jul 15 '14 at 07:17

1 Answers1

1

Yes you can absolutely do that! You can append any GET variables in your action attribute and just see your URL,it'll clearly show you the changes.

The best use of this strategy is when you want both $_GET and $_POST variables to be there.That is,you can pass variables as GET variables by appending them to action attribute and you can simultaneously pass POST variables through the form(obviously with method attribute set as "post").

Anmol
  • 303
  • 2
  • 14
  • Thank you! This is useful for the future, but I'm using a GET method while appending my query to the action attribute. But, thanks to gp., I discovered that in a GET form, the query part of the URL is completely replaced. Anything I put there previously disappears. – terpak Jul 15 '14 at 07:18
  • Yup,the url will be encoded so you might not be able to seee it but you'll still get your respective variables in $_GET and $_POST arrays.check out here http://stackoverflow.com/questions/2036858/want-to-use-both-get-and-post-methods – Anmol Jul 15 '14 at 08:37