What happens if the action field in a <form> has parameters?

前端 未结 4 1701
予麋鹿
予麋鹿 2020-12-01 10:14

Is there a well-supported, common behavior that I can expect if I do something like this in HTML:

相关标签:
4条回答
  • 2020-12-01 10:46

    Not sure, but I think it's better practice to place those variables in hidden input fields. This way it doesn't matter if your posting method is either POST or GET.

    <form method="get" action="/somePage.html">
      <input name="param2"></input>
      <input name="param3"></input>
      <input type="hidden" name="param1" value="foo" />
      <input type="hidden" name="param2" value="foo" />
    </form>
    
    0 讨论(0)
  • 2020-12-01 10:48

    You could change the method attribute in the form to "POST" with script before posting the form, so there could be a use for the query string in the action. It hardly seems to be the best solution for anything, though.

    0 讨论(0)
  • 2020-12-01 10:51

    If the method attribute is set to GET, the browser drops the querystring parameters from the action attribute before constructing the form argument values.

    So in your example, the request to the server on submit will look like: /somePage.html?param2=value&param3=value

    So no, when the method is "GET", as in your example, there's no reason to do this.

    0 讨论(0)
  • 2020-12-01 10:54

    Well, all the questions have been answered except the last, to which the answer is yes. For POST, it's allowed, but you may well find cases where it doesn't work. I've seen web servers that only allow postdata or querystring, so it's not dependable.

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