I am curious about the original purpose of the <input type="hidden">
tag.
Nowadays it is often used together with JavaScript to store variables in it which are sent to the server and things like that.
Therefore, the <input type="hidden">
existed before JavaScript, so what was its original purpose? I can only imagine of sending a value from the server to the client which is (unchanged) sent back to maintain a kind of a state. Or do I get something wrong in the history of it and <input type="hidden">
was always supposed to be used together with JavaScript?
If possible, please also give references in your answers.
Since they are not rendered visible, hidden inputs are sometimes erroneously perceived as safe. But similar to session cookies, hidden form inputs store the software's state information client-side, instead of server-side. This makes it vulnerable.
Show activity on this post. Yes, they'll get submitted unless they are removed from the document or have the disabled attribute set on them.
#3 jQuery Code To Get hidden field value by typevar getValue= $("input[type=hidden]"). val(); alert(getValue); Here in the above jquery code, we select input hidden filed by its type i.e hidden, and then by using jquery . val() we get the hidden field value.
Hidden form field is used to store session information of a client. In this method, we create a hidden form which passes the control to the servlet whose path is given in the form action area. Using this, the information of the user is stored and passed to the location where we want to send data.
I can only imagine of sending a value from the server to the client which is (unchanged) sent back to maintain a kind of a state.
Precisely. In fact, it's still being used for this purpose today because HTTP as we know it today is still, at least fundamentally, a stateless protocol.
This use case was actually first described in HTML 3.2 (I'm surprised HTML 2.0 didn't include such a description):
type=hidden
These fields should not be rendered and provide a means for servers to store state information with a form. This will be passed back to the server when the form is submitted, using the name/value pair defined by the corresponding attributes. This is a work around for the statelessness of HTTP. Another approach is to use HTTP "Cookies".<input type=hidden name=customerid value="c2415-345-8563">
While it's worth mentioning that HTML 3.2 became a W3C Recommendation only after JavaScript's initial release, it's safe to assume that hidden fields have pretty much always served the same purpose.
I'll provide a simple Server Side Real World Example here, say if the records are looped and each record has a form with a delete button and you need to delete a specific record, so here comes the hidden
field in action, else you won't get the reference of the record to be deleted in this case, it will be id
For example
<?php if(isset($_POST['delete_action'])) { mysqli_query($connection, "DELETE FROM table_name WHERE record_id = ".$_POST['row_to_be_deleted']); //Here is where hidden field value is used } while(condition) { ?> <span><?php echo 'Looped Record Name'; ?> <form method="post"> <input type="hidden" name="row_to_be_deleted" value="<?php echo $record_id; ?>" /> <input type="submit" name="delete_action" /> </form> <?php } ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With