Forms: the form tag - A Smarter Way To Learn HTML & CSS (2015)

A Smarter Way To Learn HTML & CSS(2015)

49
Forms: the form tag

It’s a rare website that doesn’t use some forms. At a minimum, you’re probably going to want to include an email form on your site to make it easy for users to contact you. Every form begins and ends with an opening <form> and closing </form> tag.

<form action="send-email.php" method="post">

[Here’s where the contents of the form, like input
fields and a submit button, are coded. We’ll start
working on these in the next chapter.]


</form>

In most cases, when a user completes a form, a program separate from the HTML file runs. In the example above, action="send-email.php" tells the browser that when the user submits the form, the information the user has entered in the form is to be sent to a PHP program on the website for processing. The program’s URL is “send-email.php.” It’s a program that runs on the host’s server. This is different from an HTML file. An HTML file is stored on the host’s server but runs in the user’s browser.

In the case of the example, send-email.php might send an email to the site owner that includes the data the user has entered. Or a program might write the data entered by the user to a database on the server. Or a program might process credit card information entered in a form.

There are all kinds of programs, written in various languages, that can process data from a form. The languages include PHP, Ruby, Python, Perl, Java, and C#. The processing programs written in these languages are outside the scope of this book, so you won’t learn anything about processing forms here, other than learning how to specify the form action in HTML tags.

But don’t be discouraged if you don’t know any of these languages. At sites like http://www.hotscripts.com/ you’ll find thousands of programs, both free and for sale, that process forms for every purpose. You don’t need to know a computer language to use these scripts. The people who write them tell you how to change a few lines of the code to adapt them so they’ll work on your site. Make a few simple changes, then upload the code to your site, and you’re in business.

The example above specifies method="post". This method is the one you use to process more than a little bit of information, and when you want to keep the information secure. The second method, get, is used mostly for search forms. You know a form is using the get method when the information entered in the form (connected by plus signs) appears in the URL after you click Submit. Here’s the URL that displayed when I searched the New Yorker site for “alice munro.”

http://www.newyorker.com/search?qt=dismax&sort=score+desc&query=alice+munro&submit=

If you don’t specify a method, the get method is used. Since this unsecure method isn’t appropriate for most purposes, you’ll usually want to specify the post method.

Find the interactive coding exercises for this chapter at:
http://www.ASmarterWayToLearn.com/htmlcss/49.html