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.

0

3 Answers 3

18

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
Yeah, this is how I would do this today, or otherwise via pretty url slugs if I really wanted stable access.
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.
-1

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

1 Comment

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...
-1

Change your code to:

<div> <form action="?brandcode=&t=" method="get"> .... </form> 

1 Comment

I can’t see that possibly helping.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.