

|

|
|

PHP Tutorials: Using a Form |

Lesson 3: Pass variables with a form
Since we have already been show
the basic principles to creating variables and how to
display them within strictly PHP documents and PHP documents
with HTML tags, it is now time to create a form to pass
variables onto the PHP page..
The Form
First, you need to create a standard form in a text program
or in your HTML program. The following is the form coding
used for this example:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Sample Form</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head>
<body> <form name="form1" method="post" action="2-1-run.php"> <table width="300" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="110" align="right">Your Name::</td> <td width="5"> </td> <td><input name="name" type="text" id="name"></td> </tr> <tr> <td align="right">Your Age:</td> <td> </td> <td><input name="age" type="text" id="age" size="4"></td> </tr> <tr> <td align="right"> </td> <td> </td> <td> </td> </tr> <tr> <td align="right"> </td> <td> </td> <td><input type="submit" name="Submit" value="Submit"></td> </tr> </table> </form> </body> </html>
|
See
how the form looks in browser window
Notice the 'name' tags, these are the names of the passed
variables which will be retrieved by the PHP page listed
under 'action'.
Now lets take a look at the PHP file that will take the passed information and convert into variables for use. The PHP listed will echo the information you inserted into the form:
<?php //you told your form to POST the inserted info, now we must retrieve the posts by use of 'name' tag $personsname = $_POST['name']; $personsage = $_POST['age'];
//remember, you can name the above variable whatever you want, as long as you remember how/where to use them
//now we can do a simple echo echo $personsname; echo "<br>"; echo $personsage;
echo "<br><br><br>";
//now lets ad text of our own echo "Your name is ". $personsname .", and you are ". $personsage ." years old.";
?>
|
Notice that each variable is created
by pulling the information from the POSTS on basis of
the 'name' tag in the form document. This same name tag
is seen within the brackets in the PHP document. You can
name the variables, such as $myage, whatever you want.
You are the one using them.
To test out the script, use the form link above. |
|
|
|
|
|
|
|
 |
|
|