What's the point of having hidden input in HTML? What are common uses for this?

后端 未结 12 1107
无人共我
无人共我 2020-12-01 14:51

I don\'t see the benefit of having hidden input? If you set the value of the hidden input why not just use that value at the point where you reference this hidden input?

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

    because you have a php while loop with alot of different objects which you can't get the id's of later and you just save them by storing them in the hidden input... also i like them to be a security check to know my users aren't messing with my post variables through tamper data

    0 讨论(0)
  • 2020-12-01 15:11

    For example, if you have a form to edit an entity from you data model, you could use an hidden input to place the id of the entity you are updating, since you don't want to have this value put in the text input field to be posted back to the server. the hidden field will be posted to the server as if it were part of your form.

    0 讨论(0)
  • 2020-12-01 15:12

    The hidden input is for when you want to send information back on a post without the user seeing the data as a UI element.

    The web is stateless - ergo, when a POST comes in, you have only a couple pieces of information to determine what you need to do; Session, Url, some ServerVariables, and the Form data passed to you.

    If a piece of data is transient that is not useful for putting in the session, sometimes it is prudent to put into a hidden field.

    They can also be useful for storing data on the client side for rich internet applications (such as a product Id that is easily accessible to use in ajax calls).

    0 讨论(0)
  • 2020-12-01 15:14

    It's just what it's name implies, a form input that is hidden from the user. It's a way of getting data to the server that the user doesn't need to see or control directly. This is especially useful for maintaining state.

    I'm working on a project right now where a user creates several items that are represented as data objects in JavaScript and serialized when sent to the server. These data items are expressed one way when displayed to the user with HTML, another way as JavaScript, and a third way when sent to the server. A hidden input is the only way to accomplish this. (Okay, fine not actually the only way, but certainly the most reasonable way).

    0 讨论(0)
  • 2020-12-01 15:18

    Consider a form that's being displayed to edit a record in a database, one technique is to bake the id of that record in a hidden input and have it submitted back so the server can read it back.

    It's also used frequently for security purposes (as genesis has said).

    Another reason might be for javascript-oriented scenarios, perhaps for non standard controls such as treeviews, where the concept of a selected node cannot be represented as a normal input. Instead, JS can manipulate a hidden field and store the node's name/id in it, so that it can be read by the server.

    0 讨论(0)
  • 2020-12-01 15:23

    If you build a tag system like the one here. You can set the values into a a hidden field.

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