PHP
Exercise 3
HTML Forms and PHP
1. In order to test form input, we need to create a
HTML file that will get us information from the user. Create a new file called form_input.html and save it in the /var/www/html
directory.
2. Insert the following lines into the form_input.html file:
<html>
<head>
<title>Form Input HTML file</title>
</head>
<body>
<form action="form_action.php"
method="GET">
Your name: <input type="text"
name="name"><br>
Your age: <input type="text"
name="age"><br>
<input type="submit">
</form>
</body>
</html>
3. Save this file and exit vi. Then create a new file called form_action.php. In this file insert the following lines:
<html>
<head>
<title>PHP Form</title>
</head>
<body>
Hi <?php echo $_GET["name"]; ?>.
You are <?php echo $_GET["age"]; ?> years
old.
</body>
</html>
4. Now open a browser and
go to http://localhost/form_input.html and enter some information into the text boxes and
click the submit button. Figure 6.2 shows the output from where I entered Joe and 22 into the
boxes.

Figure 6.2
5. There is another method you can use to accomplish the same thing. In your HTML form, you could also specify method="POST" to pass variables to a .php file. To get the variables from a POST in the .php file, you would use the variable array $_POST[], instead of using the variable array $_GET[].
6. The question often comes up as to "Which
method should I use?" The answer is not easily answered. However, here is
a small hint from http://www.cs.tut.fi/~jkorpela/forms/methods.html. The author of this website explains that the POST
method should only be used when you are updating critical information (i.e.
changing values in a database). Otherwise, if you are simply looking up
information from a database, you should use the GET method. Please read the
website for more information.
7. Now I will go over a few options that you can
choose from on input types in your HTML forms. I suggest sticking these in a
HTML file and just seeing what they look like.
·
<input
type="text" length="30" maxlength="45">
·
<input
type="password" length="20" maxlength="20">
·
<input
type="radio" name="radio_1" value="Option 1">
<input type="radio" name="radio_1"
value="Option 2">
·
<texarea
name="comment_box" rows="10"
cols="40"></textarea>
·
<input
type="checkbox" name="checkbox_1" value="Yes">
·
<select
name="pulldown_box_1">
<option value="option_1">Option
1</option>
<option value="option_2">Option
2</option>
</select>
· <input type="hidden" name="hidden_field_1" value="This is hidden">
8. A little note about the hidden fields. When you go
to the HTML page, you will NEVER see this anywhere on the page (that's why it's
hidden), but there are times when you want to pass data to a .php file and
don't want people to see it.
9. More information on forms and creating them can be found at http://www.w3schools.com/html/html_forms.asp
1. Create a HTML form that has 1 text box, 1 password
field, two radio buttons, two checkboxes, 1 pulldown menu and 1 text area for
users to input data. Then pass this information to a .php file which simply
displays all the information entered by the user. A sample of both input and
output pages is shown below.
2. You MUST comment your code to receive full credit!
|
|
|
On Your Own Exercise 3 – Sample
Answer