[php home]

Php_3: Using HTML Forms with PHP Variables (TEXT)

To start with, you will need to create an HTML form page to enter the information into. PHP is able to handle most forms including text boxes, buttons, select menus and check boxes.

Step 1: Create an HTML page with a text box for getting a person's name. Save as: input.htm

<html>
<head><title>form_php test page</title></head>
<body>

<form action="php_3.php" method="post">
What is your last name?<input type="text" name="last_name" size="20" maxlength="40">
<INPUT TYPE="submit" VALUE="Submit"> <INPUT TYPE="reset" VALUE="Reset">
</form>

</body>
</html>

NOTE: Action: The action tag tells the html page where to send the input information. In this case it will be "php_3.php" which we will create in the next step.

NOTE: Method: there are two ways of sending the information - "post" and "get".

The "get" method will pass the information to the receiving page by sending it to the URL menu - The downside of using "get" is that the information is displayed in the URL menu - not a good idea if you are asking for credit card numbers. There is also a limit to the amount of information that can be passed using "get". Generally, "post" is used.

Step 2: Create the PHP page that will process this info and return it to the browser. Create and save the document as php_3.php.

<html>
<head><title>form_php test page</title></head>
<body>

I know your last name is

<?php
echo $_POST['last_name'];
?>

<?php

echo "<center><br><a href=\"javascript:history.go(-1)\" class=\"text\">Click here to go back</a></center>";

?>

</body>
</html>
The echo statement above gets the results of the post from the form page. Specifically it is looking for the variable last_name which it got from the form we set up above.

Note: I included a line of code that will take you back to your submit page. This is not required! If you get tired of going back and forth, you could set both pages up in a frameset.

After you have saved both documents, open your input.htm document and submit your name.

php_3 exercise 1:

Expand this HTML form and PHP code to include:
First name - another text box
Gender - radio button
school - pull down menu (elem, middle, high, college)
Questions - textarea

Php_3 exercise 2:

Create a new HTML and PHP page that will rewrite this nursery rhyme based on user input.

Hey, diddle, diddle,
The cat and the fiddle,
The cow jumped over the moon.
The little dog laughed
To see such sport,
And the dish ran away with the spoon.

HTML:
Create text boxes that ask for animail 1, object 1, animal 2, etc. (do all of the words in red)

PHP:
Write a script that will output the new rhyme.