15

So I'm trying to submit a page to itself while retaining the current query string of the page.

So the page is sb.local/sb/cat.php?brandcode=JM&t=cat_items I pull off the query string and stick it back into the html form to preserve the parameters. This is the resulting form:

<form id="brand-select" method="get" action="?brandcode=JM&t=cat_items" name="brand-select">
Brand:
<select id="brandcode" style="width:207px" tabindex="3" name="brandcode" required="">
<option value=""></option>
<option class="brand-option" value="AX" data-brandid="110"> Aetrex </option>
<option class="brand-option" value="AL" data-brandid="12"> Alden </option>
<option class="brand-option" value="ETC" data-brandid="11"> Etc </option>
</select>
<input type="submit" value="go">
</form>

When I submit the form by choosing the dropdown for Aetrex (value AX), however, it goes to a url of:

sb.local/sb/cat.php?brandcode=AX in other words, it cuts out the "t=cat_items" that is in the action. It also cuts out the "brandcode=JM" but I would almost expect that since they're duplicates.

That's not what I expected, I expected that if there is a query string in the action attribute, it would append form values to that query string (e.g. sb.local/sb/cat.php?brandcode=JM&t=cat_items&brandcode=AX. Instead it seems to be replacing the query string entirely with only those elements that are in the form.

Is the form action attribute not usable for storing query parameters, only more basic url info?

Edit: Note that I can work around this by parsing every parameter and then putting each parameter into its own hidden field manually, except for any parameters that I want to allow to change, I was just hoping that there was some kind of simpler way.
I tested with a non-conflicting query string and that was replaced in whole even when there wasn't a conflict (in Firefox), so based on that it seems that query strings are useless in the action attribute of get forms? Or am I missing something.

Kzqai
  • 22,588
  • 25
  • 105
  • 137
  • 2
    Possible duplicate of [submitting a GET form with query string params and hidden params disappear](http://stackoverflow.com/questions/1116019/submitting-a-get-form-with-query-string-params-and-hidden-params-disappear) – isherwood Jan 10 '17 at 19:13

3 Answers3

17

I know this is an old question, but the solution is actually pretty simple (and neat!).

All you have to do is sending the querystring with hidden input fields in the format name="key" and value="value".

?brandcode=JM&t=cat_items would "translate" into:

<input type="hidden" name="brandcode" value="JM" />
<input type="hidden" name="t" value="cat_items" />

Completely remove the querystring from your action.

Christian Lundahl
  • 2,000
  • 3
  • 18
  • 29
  • As I mention in at the bottom of the question, I'm aware of that manual solution, but it requires parsing all url parameters and their values, then filtering out the specific ones that change [this is where the difficulty arises], and then looping over them in the form to put them into hidden inputs. – Kzqai Nov 19 '13 at 15:52
  • Yeah, this is how I would do this today, or otherwise via pretty url slugs if I really wanted stable access. – Kzqai Mar 14 '16 at 21:00
  • Actually, this isn't very hard to generate. Just iterate through $_GET (for php) or Request.QueryString (for VBScript/VB.Net) and output all the keys and values as hidden inputs. – Christian Lundahl Jun 18 '16 at 13:34
0

Change your code to:

<div>
    <form action="?brandcode=&t="   method="get">
    ....
    </form>
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
-1

You can use "POST" method instead of "GET" method for form submission, if the method doesn't matter.

Saravanan S
  • 1,021
  • 1
  • 10
  • 24
  • Hmmm, noted. In this case I was trying to use get for maximum transparency (every change gets a corresponding url change), but perhaps this disconnect is because I'm somewhat abusing the normal methods of using a form by having it be a get form... – Kzqai Jan 27 '12 at 20:46