In our examples so far, we have set variables and then used them all in
the same code. This doesn't make much sense because in those instances,
we could have just hard-coded the values instead of using variables.
Let's get some real mileage by creating HTML forms to gather user input,
turning that input into variables, and then doing various things with the
information that we just collected. Maybe I'm a dork, but I think this is
fun.
No sense in sitting around waiting let's go ahead and make a Web page
that collects your favorite dirty word and displays it on another page that
tells you what a pervert you are. All of this gives a page that looks a lot like this.
First, we make the form page, which is regular HTML with a form in it.
I'm calling mine "badwords_form.html," but call yours whatever
you like. (If you want a good primer on HTML forms, read Jay's Good
Forms tutorial.)
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20. |
<html>
<head>
<title>My Form</title>
</head>
<body>
<form action="bad_words.php" method=post>
My name is:
<br> <input type="text" name="YourName">
<p> My favorite dirty word is:
<br> <input type="text" name="FavoriteWord">
<p>
<input type="submit" name="submit" value="Enter My Data!">
</form>
</body>
</html> |
This is a regular HTML form. The important pieces are as follows:
Line 7: the HTML that reads action="bad_words.php" tells the browser
which PHP document will process the results of the form. That is to say,
in a minute you'll create a document called "bad_words.php" which
will be the little engine that makes the result page happen. (We'll get
to the method=post part later on.)
Line 10: input type="text" determines that the form element which
we want here is "text" or a text box (we could also have a radio
button, check box, etc.); name="YourName" determines that whatever
the user types into the text box will become a variable that we have called
"YourName." This is what ties together forms and variables each
form field can set a variable to be used however you want.
Line 13: here you have another text input that sets a variable called "FavoriteWord"
which is the user's favorite dirty word.
Line 16, 17: This code makes a submit button with the text "That's
Right!" and ends the form.
Okeedoke. So this form will collect the unassuming user's name and favorite
bad word, but now what do we do with it? Let's take the variables she set
and echo them back in another context on another page.
On line 7 of the HTML above, we told the form to head on over to bad_words.php
once the submit button was hit. This is what bad_words.php looks
like:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18. |
<html>
<head>
<title>Perv!</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<p>
Hi <?php print $YourName; ?>
<p>
You like the word <b> <?php print $FavoriteWord; ?> !?! </b>
<p>You oughta be ashamed of yourself!
</body>
</html>
|
See how this form passed a variable along from the form page to the PHP
file? You have not seen the last of this.
Get versus Post
So far, we've used the "Post" method of passing form data as
opposed to the other method, "Get." This is the part of the form
code that reads <form action="bad_words.php" method=post>.
The difference between these two is that the "post" method transparently
passes along all the information the page has gathered, whereas the "get"
method will pass all that info along as part of the URL (in the form above,
this would look like: http://rocketboy/webmonkey_article/bad_words.php?YourName=bob&
FavoriteWord=crikey%21&submit=Enter+My+Data%21 see how the info the user entered about his name and his favorite word
get added to the URL?)
next page»