So far, all that we've talked about involves presenting information to users using HTML. What about if you want your website visitors to send information to you? We are now going to look at how to collect information from your visitors. This is done on the web using HTML Forms. What is a form? I'll answer that by giving your some visual examples. What you see above are called forms. Here are the names of these respective forms:
For now, let's learn how to create these forms. Any kind of form at all that you create must be enclosed in the form tag <form> </form> All other tags go inside the form tag, like this <form>...</form> Text formMost form elements are created using <input> tag.You then add the attribute <input type=""> to obtain a particular type of form
<form> The above html will produce Here is a question for you? Where is the closing tag for the <input> tag? The answer is, it doesn't have a closing tag. Other attributes of TEXT formSize will vary the horizontal width
<form>
<input type="text" size="60">
</form> This produces maxlength will limit the number of characters the user can type into the form. Type some text into both text forms below, make it at least ten characters each.
You will notice that you can only type five characters into the second text form. It is done with: <form><input type="text" size="50" maxlength="5"></form> value puts some default text inside the form
<form>
<input type="text" size="35" value="My default text">
</form> name attribute will differentiate one form element from another. A form is likely to contain more than one form element. We need a way to know which form element corresponds to a particular value.
<form> ButtonsA button is created using the <input type="submit"> tag.
<form>
<input type="submit">
</form> If you want it to say something else, e.g. Click here to send, just add a value attribute.
<form>
<input type="submit" value="Click here to send">
</form> Before we look at the other types of form elements, let's create a complete form that we can use to collect information from a webpage. <form> <input type="text" size="20"> <input type="submit" value="Click to E-mail info"> </form> This produces If you click on the button, it's supposed to send the info inside the form to you by email. Go ahead and click the button. Nothing happens. To make the button work, we have to add action="" attribute to the <form> tag, like this <form action="">. The action attribute tells the web browser what to do when the submit button is clicked. In our case, we want the form to be sent to us by email. In that case, we must add the email address to be sent to, like this <form action="mailto:email_address"> Just change email_address to your real email address, and users visiting your webpage can send you info by email. To send the same message to more than one email address, just separate all the addresses by commas. E.g. mailto:email_address1,email_address2,email_address3. The full html then is:
<form action="mailto:email_address"> We can also label our form, so the user can know what info we want.
<form action="mailto:email_address">
Now type something into the form and click the button, it should try to send the form by email.
Note that we have used mailto: here for collecting user info. This is a totally unreliable method. Depending on the user's system, the form may be sent by email, or a blank email interface may be opened on your screen. Next, we must tell the browser how to encode the info inside the form by adding another attribute ENCTYPE. One can see that ENCTYPE means ENCoding TYPE. In our case, we want the info sent as plain text, instead of binary. We will learn about other encoding styles later.
<form action="mailto:email_address" ENCTYPE="text/plain"> One last attribute to add to the form tag is METHOD. Two values are possible for this attribute: POST and GET. In most HTML forms, you should use POST. The use of GET is beyond the level of this turorial, but to explain briefly, it allows the info in the form to be carried as a query string. This becomes very useful when you learn web scripting.
<form action="mailto:email_address" ENCTYPE="text/plain" method="post"> Adding a subject When you use the form we have so far to send info by email, the SUBJECT in the email will be a default value. That default value depends on the browser. Netscape for example, will say Form posted from Mozilla. If you want to change the subject, do the following:
<form action="mailto:email_address?Subject=Info from my HTML page" ENCTYPE="text/plain" method="post"> Textarea<input type="text"> only allows you to enter one line of text at a time. With TEXTAREA tag, you can type multiple lines of text.Instead of using <input type=""> tag as we did for text form, you use <textarea> </textarea> for creating a textarea. Also, note that textarea has a closing tag.
<form action="mailto:email_address?Subject=Info from my HTML page" ENCTYPE="text/plain" method="post"> This produces Other attributes of TEXTAREAcols will change the number of columns, i.e. alters the width.rows controls the number of rows
<form action="mailto:email_address?Subject=Info from my HTML page" ENCTYPE="text/plain" method="post">
name does the same thing as in TEXT form. TEXTAREA does not have a value attribute. If you want a default value in your textarea, just type it between the opening and closing tags, like this <textarea>This is a default value<textarea>.
wrap="off" causes the text in the textarea to be one long text. The text also gets sent as one long text. This is the same as nowrap. Type some text into the box below and see what happens.
Note that the Browse... button is created automatically with INPUT TYPE="FILE". Radio ButtonsRadio buttons are used when you want a user to select only one option out of several. The options are usually mutually exclusive, i.e. only one option can be correct out of the available options.
Here is an example: Since only one of the options can be selected, it means all the radio elements must have the same name attribute. If they have the same name attribute, how then can we differentiate between them? We differentiate with the value attribute. The html code for the above is:
<input type="radio" name="age" value="18-29">
With the above code, non of the radio buttons will be selected the first time the page loads. <input type="radio" name="age" value="18-29" checked> CheckboxesCheckboxes are used when a user can select more than one option from several options.As opposed to radio buttons that all have the same name and different values, checkboxes have different names, and the same value. Example:
Here is the code:
<form>
<input type="checkbox" name="Australia" value="Y"> Australia The value can be anything you want. If you wish to pre-check some of the boxes, just add checked to the tags. E.g. <input type="checkbox" name="Asia" value="Y" checked> Asia SelectsAnother type of form element is the select.Here is an example. |