Create a Simple Form in HTML
The introduction of HyperText Markup Language (HTML) and the HyperText Transfer Protocol (HTTP) brought about the Internet revolution known as the World Wide Web. In those early days, all web pages were static by nature, meaning pages looked the same every time they were viewed.
Interactive content did not exist. Web designers were limited to displaying the same information over and over. There was also no way to collect feedback from viewers or generate sales online. The introduction of HTML forms gave authors a method to solicit feedback and resulted in the introduction of e-Business technologies and online catalogs.
The goal of any Webmaster is to create sticky Web sites. Creating interactive Web pages is a must to make a site sticky. What is a sticky site? This is a site that users visit over and over. One of the simplest ways to promote interactivity in a site is to use <a href="https://coderwall.com/p/qg2asw/pure-html-semantic-acronyms/">HTML</a> forms. Forms provide areas for users to supply input to change the content of the site, provide feedback, request information, or take advantage of other functionality.
Before we go any further, I should explain that this hub assumes that you understand how to create a basic Web page using essential HTML tags and how to create a table using HTML. Further, the code snippets included in these tutorial hubs only include the code for the demonstrated elements, in this case -- a form.
Let's create a simple form using HTML to display on a Web page. This particular form will render using a table format, although we could use a more free-flowing format, such as the paragraph. To view the form that appears in the following code snippet in a Web browser, insert the code inside the body element of an HTML document.
<center>
<form action="retrieveInfo.asp" method="get" id="contactForm" name="contactForm">
<table>
<tr><th colspan="2">Simple Contact Form</th></tr>
<tr><td>First Name: </td><td><input type="txt" name="fname" /></td></tr>
<tr><td>Last Name: </td><td><input type="txt" name="lname" /></td></tr>
<tr><td>E-mail: </td><td><input type="txt" name="email" /></td></tr>
<tr><td></td><td><input type="submit" value="Submit" /></td></tr>
<tr><td colspan="2">Submit Form for more Information</td></tr>
</table>
</form>
The HTML Form Element
The above code snippet for a simple form introduces a couple new elements to our HTML arsenal. These elements are the form and input elements, identified by the HTML <form> ... </form> and <input> ... </input> tag pairs. Everything between the opening (<form>) tag and the closing </form>) tag are part of the form.
Notice that the form element includes a number of attributes or descriptive items within the tag. These attributes include the following:
action="retrieveInfo.asp"
method="get"
id="contactForm"
name="contactForm"
The attributes instruct the browser on what to do with the form data supplied by the user and identify the form for later use. The action attribute works with the submit input element covered below and instructs the server receiving the form data to handle the data using the retrieved info.asp server-side script; more on server-side scripting in a later hub but understand that most HTML documents originate from Web servers and HTML forms may then communicate with those servers using forms. Therefore, when the user submits the data in the form to the server, the server handles the data using the script file identified by the action element.
The method attribute, in this case, get, instructs the web browser how to supply the data to the server. There are two basic methods for browsers to communicate with web servers. Those two methods get and post. The get method includes the data in a URL supplied to the server that requests or gets, another page from the server. The server then processes the request and supplies the results back to the web browser to display for the client or site visitor. We did not use the post method and the description of that method is outside the scope of this hub.
The two attributes that are left are the id and name attributes. These attributes simply identify and name the form so that a client-side script, such as JavaScript, can refer back to the form after the form has been displayed or rendered by the browser.
The HTML Input Element
The HTML input elements identify areas where the page viewer may enter and submit data. The input element includes attributes on the lines where the elements are displayed. The most important attribute is in this case the type attribute. The type attribute of the input element identifies the type of input the viewer may provide.
The two types of input in this form are txt and submit. The txt type creates a text box where the user may type in data. This form includes three text boxes: first name, last name, and e-mail text boxes. The form user may type data into the text boxes then submit the form to the server by clicking on the submit button created by the submit input type. When the user clicks on the submit button, the form data is submitted to the server using the method identified by the method attribute in the above form element.
Note: There are other attributes and input types that may be used when creating forms. And many styles may be used to modify the format of forms or how they display. Complete information on HTML is available from w3schools.com.
We have now created a simple form that requests contact information from a user. For ideas on what you can do with the data in a form within a browser, continue to retrieve data from an HTML form.