6

When i try the following structure, it does't send id=value

<form action="some.php?id=value" method="get">
   <input name="name1" value="value1">
   <input type="submit">
</form>

I know that i can send id=value in hidden field, but it's interesting, why it doesn't allow such structure?

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
Simon
  • 22,637
  • 36
  • 92
  • 121

7 Answers7

6

It's because the "method=get" section of the form implies that the query values have to come from the form values.

The collection which contains "id=value" is overidden by the collection containing the form values.

This behaviour seems to be built into each browser, so I expect that it's part of the HTML specification.

Update

Ahah:

This has been asked before: submitting a GET form with query string params and hidden params disappear

To Quote:

As the specifications (RFC1866, page 46; HTML 4.x section 17.13.3) state:

If the method is "get" and the action is an HTTP URI, the user agent takes the value of action, appends a `?' to it, then appends the form data set, encoded using the "application/x-www-form-urlencoded" content type.

Community
  • 1
  • 1
seanyboy
  • 5,623
  • 7
  • 43
  • 56
1

If your form method is POST, then you will not see id as part of the POSTed values, you can use the QueryString collection to access it.

D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
  • 2
    You can't bookmark a POSTed page though. Perhaps he wants a search function where people could bookmark the results, or something similar. – Austin Fitzpatrick Aug 23 '10 at 15:11
1

Isn't it because this would send a request to

some.php?id=vaule?name1=value1

instead of

some.php?id=vaule&name1=value1 ?

As far as I know a query string only has one "?" and a "?" is appended to the URL when you use GET parameters.

Austin Fitzpatrick
  • 7,243
  • 3
  • 25
  • 22
  • Actually, it's not correct; as the if=value part is not sent. See [Seanyboy's answer](http://stackoverflow.com/questions/3548795/html-form-why-action-cant-have-get-value-in-it/3548989#3548989) or [mine](http://stackoverflow.com/questions/3548795/html-form-why-action-cant-have-get-value-in-it/3549258#3549258). – GreenMatt Aug 23 '10 at 17:43
0

This behaviour is really annoying. One workaround is to drop the form completely and build your url by hand on the client side.

I used jQuery in one of our .NET projects to achieve this. It is a search mask with dozens of parameters. One of them is a range slider (min/max) values. You use the slider to select two values and have to press on the search button next to it.

<!-- Input fields (slider code removed) -->
<div class="input-fields">
    @(Model.Unit) <input type="text" id="@(sliderMinId)" value="@(sliderSelMinVal)" />
    to @(Model.Unit) <input type="text" id="@(sliderMaxId)" value="@(sliderSelMaxVal)" />
    <input type="button" id="@(sliderButton)" value="Search" />
</div>

And the event handler for the button:

UrlHelper h = UrlHelperExtensions.CreateRequestAgnosticUrlHelper();

/* Search-Button Click-EventHandler */
$("#@(sliderButton)").click(function(){
     @{
      string url = h.Action("Index", "SearchAction", new RouteValueDictionary(searchParams));
      string urlSeparator = url.Contains("?") ? "&" : "?";
     }
     var url = '@Html.Raw(url + urlSeparator + sliderInfo.AssociatedFieldName)=' + $("#@Html.Raw(sliderMinId)").val() + '%20-%20' + $("#@Html.Raw(sliderMaxId)").val();
     window.location.replace(url);
});

This is what happens:

  1. Build an Url containing all previous parameters.
  2. Append your new "form" parameters to it. In this case they are added as one parameter (min-max). Don't let that confuse you.
  3. Redirect to the new Url.

This is totally awkward for something that could be so simple... I hope this helps.

Krisztián Balla
  • 19,223
  • 13
  • 68
  • 84
  • What is `UrlHelperExtensions.CreateRequestAgnosticUrlHelper` ? – Kiquenet Jan 26 '17 at 08:03
  • @Kiquenet: Unfortunately I no longer have access to this code. If I recall correctly the method creates an UrlHelper object that does not include parameters from the current request. – Krisztián Balla Jan 29 '17 at 12:26
0

What are you trying to do? What is the purpose of doing it this way? If you set the method of your form to "GET" and then have the hidden field in your form with a name of "id" it will append the get variables to the end of the action and create a url for you.

Update: if the browser allowed querystring params to remain while appending params from a from GET, then there could be a collision between names. You could easily add another querystring paramater to a URL without realizing it collides with a form input name.

To avoid this, if you are using GET action in your forms, pass all additional params as hidden inputs.

D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
Aaron Hathaway
  • 4,280
  • 2
  • 19
  • 17
  • 1
    i want to send some fields with get, at the same time i don't want to lose the current value of my get variables. for example if now i'm in `some.php?lang=am` after submiting i need to redirect to `some.php?lang=am&fild1_name=field1_value...` – Simon Aug 23 '10 at 15:07
  • You could have collisions either way. It's just as easy to create two inputs with the same name. The least astonishing behavior would be to allow parameters in the URL and simply overwrite them if an input with the same name exists. – Lèse majesté Aug 23 '10 at 15:25
0

Specifying values in the action URI will have no effect, as the values in the URI will get overridden. To demonstrate, try the HTML and PHP code below. The form_test.html file could be renamed, but obviously, the PHP script needs to be named regurgitate.php (or the action in the form needs to be edited).

form_test.html:

<html>
<head>
<title>Test of setting form action url</title>
<body>
<form action="regurgitate.php?id=value" method="get">
<input type="submit" name="Submit">
<input type="hidden" id="test" name="test" value="test">
</form>
</body>

regurgitate.php:

<html>
<head>
<title>Test PHP script for testing form action</title>
<body>
<?php
echo "In regurgitate.php<br>\n";

foreach ($_GET as $k) {
  echo "\$_GET['$k']: >" . $_GET["$k"] . "<<br>\n";
}

?>
</body>

When the submit button is clicked on form_test.html, the URL in my browser becomes something like:

http://www.example.com/regurgitate.php?Submit=Submit+Query&test=test

Note there's no "id=value" in the URL.

The output is:

In regurgitate.php
$_GET['Submit Query']: ><
$_GET['test']: >test<

GreenMatt
  • 18,244
  • 7
  • 53
  • 79
0

A work around if you know what SUPERGLOBALS that you are gonna use, try this

<?php 
if (isset($_GET['id'])) echo "<input type='hidden' name='id' value='" . $_GET['id'] . "'/>";
?>

Put this anywhere in your form and it would add the hidden field for you. Works for me.

If the previous page didn't contain a query then php would ignore adding the hidden field

sevenseacat
  • 24,699
  • 6
  • 63
  • 88
DaBomb
  • 11
  • 1