tags:

views:

83

answers:

5

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?

Thanks

+1  A: 

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.

RedFilter
i use "get" method
Syom
Use the POST method instead, it has many advantages.
RedFilter
@RedFilter: 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
it's used for search engine, so it's most preferred to use get, i think:/
Syom
@Syom: see my update.
RedFilter
A: 

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.

Aaron Hathaway
Syom
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é
+1  A: 

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
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
+3  A: 

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: http://stackoverflow.com/questions/1116019/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.

seanyboy
A: 

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&amp;test=test

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

The output is:

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

GreenMatt