PHP: Lesson 3 Forms

A Tutorial

Authors

<-- Back to PHP: Lesson 2 Variables and Assignment
--> Continue to PHP: Lesson 4 Grab a Web Page into PHP


Sometimes we want to work with a value which we don't know in advance, and so which might take on a number of different values.  For example maybe you want the user to type in their name, but you're not going to know their name in advance.  In the case where we will be obtaining this directly from an interactive user, we use forms.

Anyone who has been on the Internet, longer than an hour, has probably encountered at least one form.  Enter your address, your birthdate, choose a color, submit button, are all parts of forms.  Here is a simple form, let's save it at MyFirstForm.html

<html><title>My First Form</title><body>
<form action = "displayform.php" method = "post">
Address: <input type = "text" name = "address" />
Birthdate: <input type = "text" name = "birthdate" />
<input type = "Submit" />
</body></html>

Copy this to the source of a webpage, and then refresh, and you should see a form like
Address:
Birthdate:
Submit

The Submit is a button, but I can't figure out how to display it here on Knol.  Knol does not allow the use of standard HTML forms, perhaps you have to use Spreadsheet Forms instead.

At any rate, our form here says that when the user hits the Submit button, that the form should execute whatever is at displayform.php using the "post" method (which sends the answers invisibly).

Now what you need is to create your displayform.php page.
So on this page you might have
<html><body>
Your address is : <?php echo $_POST["address"]; ?> <br>
Your birthdate is <?php echo $_POST["birthdate"]; ?
</body></html>

Pretty boring, but you get the idea.  The answers the user gave, are collected at MyFirstForm.html and then sent, invisibly, to displayform.php which will display something like
Your address is: 123 Main St
Your birthdate is 01-01-1940

Let's say your website is called http://www.clowns.com, then the URL you will see in your address bar will simply be:
http://www.clowns.com/displayform.php


Sometimes however, you want the user to be able to bookmark the exact result page, so they can jump directly back there.  For example, I'm searching for real estate in San Francisco, so I enter that city and click Submit.  I might, as an end-user want to bookmark that page.  But since the POST does not store the answers I gave, I would just end up on the main page of the site, not on the San Francisco results page.  So for the purposes of bookmarking results pages, there is also a method called "get".  It would work exactly the same way, except where you have POST above, you use GET


<html><title>My First Form</title><body>
<form action = "displayform.php" method = "get">
Address: <input type = "text" name = "address" />
Birthdate: <input type = "text" name = "birthdate" />
<input type = "Submit" />
</body></html>

So on this page you might have
<html><body>
Your address is : <?php echo $_GET["address"]; ?> <br>
Your birthdate is <?php echo $_GET["birthdate"]; ?
</body></html>

The answers the user gives are, using the GET method, embedded in the URL, so if they bookmark it, and then come back later, the URL directly tells displayform.php what answers to show.  Be sure that you do not use GET with any sort of password.

In the case of using GET, the URL you will see in your address bar will now be:
http://www.clowns.com/displayform.php?address=123_Main_St&birthdate=01-01-1940

Bookmarking that URL and then jumping directly to it, should display the exact same page results as if you had used the form page.  So you can see by this, that if you find some website which is obviously using the GET method, you can build your own custom URL's and use them programmatically (in a program).


<-- Back to PHP: Lesson 2 Variables and Assignment
--> Continue to PHP: Lesson 4 Grab a Web Page into PHP