Join Bill Weinman for an in-depth discussion in this video Using hidden fields, part of CGI Essential Training.
There is another type of form element here that you cannot see and it's actually invisible and that's called hidden fields. To explain why hidden fields exist and what they do, I am going to take just a moment and talk about HTTP. HTTP is the protocol that your browser uses to talk to the server and the server talking back to the browser. HTTP is what's called a state-less protocol and what that means that every time you connect to the server and the server responds back to the browser, that is an entirely separate session.
Even though you may be doing something that takes several different connections that might cover several different pages that you consider a session and even the CGI application might consideration a session, as far as HTTP is concerned, every single connection is a separate session and it does not have any innate way of carrying on connections between sessions and in fact, there is no way to know even by IP address whether the next connection that you receive is the same user as the previous one.
So this creates a problem and hidden fields is a great way of solving that problem. Going over to our HTML page, coming down here to the bottom, we are going to put in a hidden field. Now hidden fields are content and so they must go some place in the HTML file where content is allowed. So any place that text is allowed is some place where the hidden field can be and also it must inside of the form container.
So I tend to put it right next to the Submit button because it's a convenient place and it's not like it's actually going to get in the way of anything on the page. It doesn't actually take up any space. So we're putting a hidden field here. Hidden fields use the input element with type = "hidden" and again, a name and a value. So there is our hidden field. It has the name and a value and we will go ahead and save and reload in the browser and I am just going to submit the form without typing anything in and we can see here is our hidden field right there.
So even though it didn't display on the page, it didn't take up any space on the page. We still got the value there in the form response and so that means that you now have a way of storing something on a page which can carry from page to page and create a virtual session and you will see an example of how this is done and why this is done in our case study later in this course.
- Understanding the server requirements for CGI
- Installing CGI scripts on Unix and Windows servers
- Coding form elements into an HTML page
- Keeping context with hidden fields on a web form
- Learning best practices for creating CGI content