Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Forms in XHTML and HTML are how you provide interactions between a user and a web server. So they look like this. You have places where you can type things. You have buttons that you can press, and you have more places you can type things and more buttons you can press. There are different types of elements in a form and these are called widgets. So you can type text into a text widget, you can press radio buttons, and radio buttons are the kinds of buttons that are mutually exclusive.
In other words, I can only press one radio button at a time in a group of radio buttons, and they are groups together, so that you can have one choice out of several. Checkboxes are widgets that you can check and uncheck at will and they do not group together. So you can always check as many or as few as you like. Then this is a submit button. So when I'll click on this button, it will actually submit this data to the server, and then a program on the server takes over, and does whatever it's supposed to do with the data from the form. So there are a couple of things to understand here.
The most important is this is a two-part process. There is a front end and there is a back end. The front end is the XHTML or HTML that creates the form, and we have that here in this text editor here, so that you can see how to create these widgets. This course is about the front end. The back end is what the server does with the data once you submit it to the server. The back end is usually written in a program like Perl or PHP or something else. It accepts the data from the web browser, from the form that the user has filled out on the web browser using a protocol called CGI stands for Common Gateway Interface, and it's simply the very small, very simple specifications of how the browser talks to the server and passes back and forth this information that's in the form.
So this is about the front end. Now obviously the form is no good without some kind of a back end, and you need to understand that the process of installing a script on the back end is something that varies from server to server. I can give you the script, but I can't necessarily tell you how it installs on your server. The scripts that I have provided in the Exercise Files are written in Perl, and I have a separate course on Lynda.com about how to install Perl scripts on a server. Again, because all servers are different, it doesn't cover all possible situations, but it covers a lot of them.
And if you are interested in how to do that, you can take a look at that course using Perl with CGI, I believe it's called, and that will teach you how to do that. In the meantime for the purpose of testing your form, I have installed a small script on my server, and all it does is it gives you back exactly what you put into the form, so that you can experiment with your form fields, and you can at least design a form and follow along in this lesson. So I'm just going to go ahead here and click on Send, and you will see what that does is it just echoes back what you have you typed in the form fields and gives you some other information that probably doesn't mean a whole lot, but in the context of the server, it does.
So that is the there, so that you can follow along in this lesson. I've included that script in the Exercise Files, and I have also included a mail form script in the Exercise Files, which if you have somebody who knows how to install such things, you can install that on your server and use this form to actually send feedback to a given email address that you provide in the form. I'll talk a little bit about the CGI programs in the next lesson in this chapter, but this chapter is really about the front end. It's about how to do the forms in XHTML and HTML. So let's take a quick look at the form here, and what it does, and how it works.
This is form in XHTML, and it starts here with a form tag, and form tag has a couple of attributes. It has a method attribute, and it has an action attribute, these are required. Method = "post". There are two choices here. There is get and post, and we'll talk about those in a minute. The action attribute gives the location of the CGI script on your server in a URL. In this case it's my server, bw.org is my server, and it's this test.cgi, and again, you are welcome to use that just for the purpose of following along in this lesson.
Then we have the form elements. The forms elements are typically like this in an input tag. There are a few exceptions and we'll see that. The input tag here, in this case, it's type = "text" and that makes a text field. So we have here this little text box, one line of text, and that is done with this input type = "text" and name = "Name". We will talk in a moment about what this name attribute does. So here is another text box.
I'll scroll down here a little bit, and here is our radio buttons. So we have input type = "radio", name = " Retail", value = "Consumer" and checked = "checked" and then here is another radio button. Input type = "radio", name = "Retail", value = "Store". So you will notice that this one here says checked = "checked" and all that means is that. That's the button that is selected by default, and this name = "Retail", you will see, we used the same name in both of the radio buttons.
That's how they are grouped together. So you can only have one radio button selected at a time in a given group, and the group is defined by having the same name. So if they have the same name, only one of them at a time is going to be allowed to be selected. So we'll look at this in the browser, and here we have these two. Now I'm going to reset this form. If I just press Reload, it doesn't actually reset the form. It will reload the HTML, and it will show any changes here, but the data remains the same.
So if I want to actually reload the form, and reset it to its default position, I actually have to go up into the location bar and select it, and press the Enter key on my keyboard, which I'm going to do right here. That actually resets the form to its default state. Different browsers work a little bit differently, but most of them require something like that in order to reset a form. So now it's in its reset state, and you will see that the Consumer button is pressed, because I have this checked attribute here. If I take that out and save it, and then I'll reset the form again by going up to the title bar and pressing Enter.
Now you'll see that neither of the buttons are pressed. I need to press one. I don't have to. I can press neither of them. This is the only way to get neither of them to be selected. If I now select one, or select the other one, there is now no other way I can click outside of it. It doesn't unselect it. There is no other way to get none of them to be selected, but there is always going to be one at this point, and no more than one. So I'm going to go ahead and put that checked back in here, and save, and we'll go back and reset, and there we are.
Moving on, here are the checkboxes, and again, if I want to make one of these checked by default, I can use that checked = "checked" syntax. Here each of them has a separate name you will notice. The name attribute is different in each one of them, and you will see that the type = "checkbox" and that's what makes them checkboxes like here. You will see also that the text that's related to both the radio buttons and the checkboxes is separate. It's outside of the form element. It's done in the HTML. So I have word consumer here before the radio button for Consumer. I have the word retail stored here before the retail store, and all of these input elements are inline level elements.
So you go right in there with the text, and they just flow just like the text. So that's how that works. I just I want to show you, if I put in here checked = "checked", you will see that now this Hobbyist checkbox will be selected by default. I'll go back here to the browser, and I'm going to reset the form by selecting the location bar, and pressing the Enter key, and you see now that Hobbyist is checked by default. I can uncheck it if I want to, and I can check something else if I want to, but it's checked by default, because I had that checked = "checked" default in there, in the XHTML.
So I'll go ahead and take that out, and Save, and we'll move along here. We have another text box here. Input type = "text" and then we have one called textarea, and that's right here. Textarea is a container, and it creates this text area here, which you can type a lot of stuff into. You can type a lot of stuff in here. And if I just copy and paste that, you will see that eventually it gets a scrollbar after it fills up for a while.
So you can actually put up a lot of stuff in a text area. The dimensions of the textarea are specified here with cols = "30" and rows = "5". You can do different things within in CSS if you like. The contents of the container here, you'll notice it has a begin tag and an end tag is the default value. So if I put in here, This is the default for the textarea, and I click Save, and then I go in here and reset the form by selecting the location bar, and pressing Enter, you will see that now I get the default value in the textarea.
So that can be useful, if you want it to default or something. We'll go ahead and take that out for now, and Save. Finally, on this page you will see we have this button, which submits the form. It's the Send button down there at the bottom. This is type = "image". So a type = "image" acts like a submit button. Instead, you can say type = " submit" and I'll click Save here. Instead of this image here, we'll get a submit button, a normal submit button as we would see in our operating system.
So I'm just going to press Reload, and we'll see that the image now changes to this submit button that says Submit Query. That's the default text for that. You can change that with a value attribute. You can have it say value = "Label goes here", and if I save that, then the button will say label goes here when I reload. See, label goes here. So I'm going to go ahead and put this back the way that it was, and that's you create a normal submit button. Most people prefer the image buttons instead, so they can create their own image.
They can create their own button. Most people prefer to do it that way. You can even do things like creating rollovers using CSS and a lot of people like to do that. Those are the major elements available in a form. Those are the major Form widgets available.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99937 Viewers
56 Video lessons · 113027 Viewers
71 Video lessons · 81879 Viewers
131 Video lessons · 39286 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.