Form submission with a ?key=val query string alrea

2019-01-20 08:43发布

问题:

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.

回答1:

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.



回答2:

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



标签: html-form