john-charles
john-charles

Reputation: 1469

HTML Form submission with GET and extra arguments

Here is my situation, I have a page with a form on it. The form is just a query, so I'm using the "GET" method. I have a variable which I am passing around as a token, the variable is of the form "x=2&y=5" since the variable is passed into my page as an encoded url it is just extra work to parse it out to a dictionary of {'x':2,'y':5}. What would be ideal is something like the following:

<form method="GET" action="/path/to/action/?x=2&y=5">
    <input type="text" name="some_other_val">
    ... more inputs here etc.
</form>

Unfortunately when I do it this way the browser erases the ending x=2&y=5 and just appends the form fields as the arguments. The reason that I don't just use <input type="hidden" name="x" value="2"> is because like I said the string is already in that form, and I would need to specifically decode it. Adding the value to the end of the action url is simpler. Any suggestions would be greatly appreciated. Please note I found a very similar thread about this here adding URL parameters to a form PHP HTML but none of the solutions presented solve my basic problem. I also tried the tip of appending an '&' on the end but it did not work either! Is it possible to do what I want?

Upvotes: 0

Views: 2040

Answers (1)

Oded
Oded

Reputation: 499062

You should use hidden form elements for the extra parameters as you have described:

<input type="hidden" id="x" value="2" />
<input type="hidden" id="y" value="5" />

This ensures that the parameters will appear correctly on the URL when using GET instead of getting removed.

I don't quite get your argument against them, as if you can append them to the form action you should be able to simply change that to hidden form elements easily enough.

Upvotes: 4

Related Questions