HTML Form: why action can't have get value in it?

前端 未结 7 2246
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-19 12:07

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

相关标签:
7条回答
  • 2020-12-19 12:24

    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.

    0 讨论(0)
  • 2020-12-19 12:27

    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.

    0 讨论(0)
  • 2020-12-19 12:31

    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<

    0 讨论(0)
  • 2020-12-19 12:32

    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.

    0 讨论(0)
  • 2020-12-19 12:36

    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.

    0 讨论(0)
  • 2020-12-19 12:41

    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

    0 讨论(0)
提交回复
热议问题