Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
One of the main problems with the way we bpages, work and indeed a problem with the HTTP protocol, is that it's stateless. Every time you send a form response to a server it's a completely new connection and the server has no indication of the context of your session. There is typically no identifying information in the connection to indicate that it's related to another recent connection. To make matters even worse, there could be a number of users using different sessions on the same resource, all from the same IP address or a proxy server.
Of course, this is why HTTP cookies were invented, but for many uses there's a simpler solution. HTML form supports an invisible element called hidden, sometimes called hidden fields. Let's see how this works. I am going to make a working copy of forms.html and we are going to rename this to forms-working.html. I am going to open this in the text editor here. You'll notice it's using the same forms.css and forms.js that we've been using throughout this chapter. And I'm going to go ahead and open this in our browser as well, and we'll open this with Firefox.
Here's our little form, and you notice that it has a text field, a password field, some checkboxes, and a set of radio buttons. And when I fill it out and press the Big Red Button, you'll notice that we get all of those values. Now we can add to this, and I'm just going to put it down here, and it's not going to go in a paragraph or anything because it doesn't actually display. We can put in some hidden elements. And these are constructed, again, with input. In this case, it's type="hidden," and we can give it a name.
Let's call this one userid and a value="12345," and I'm just going to copy this, give it a few more here. Let's see. We'll have a page and an action and a color, blue, right? Now when I save this and come back over here to our browser--I'm going to reload and I'm just going to press the Big Red Button here-- you'll notice that these hidden fields, we actually get these named/value pairs, and that works the same obviously when you submit it to a CGI script on a server.
And so using these hidden fields, you could keep track of a session. You can keep track of userid. And the other great thing about them as opposed to the cookies, is that these are entirely not persistent, so when this session is done, these are gone and nobody is keeping track of anybody. Of course, if you want to on the other end, you can keep a database. You can have a cookie for your userid. I mean you can use a combination of technologies, but these are a beautiful way of just keeping context of a session between pages in a CGI scripting application.
So, hidden fields are great way to keep context in this session. Unlike cookies, they can be coded right in your HTML and they're not persistent. They can be generated by a script, and they work really well for keeping track of CGI sessions.
Get unlimited access to all courses for just $25/month.Become a member