First powerful feature of PHP that we are going to introduce here is the way how PHP handles HTML forms and variables from such forms. With these variables, you may accomplish many features, such as: sending web-based email, outputting information in Web browser, pass data to and from a database. In order to demonstrate these capabilities, let's assume we have the following HTML form:
<html>
<head>
<title>General Preferences</title>
</head>
<body>
<center>
Would you like to tell us your preferences?
<p>
<table width = 400>
<tr>
<td align = right>
<form action = "action1.php3" method = "POST">
Your name:
<br>
<input type = "text" name = "name" size = "20" maxlength = "30">
<p>
Your age:
<br>
<input type = "text" name = "age" size = "20" maxlength = "30">
<p>
I prefer:
<select name = "preference">
<option value = News>News
<option value = Movies >Movies
<option value = Sports>Sports
<option value = Editor>Editor
</select>
<p>
<input type = "submit" value = "Send">
</form>
</td>
</tr>
</table>
</center>
</body>
</html>
Note, that the action of this form points to a PHP file called action1.php3. This file contains a number of commands that will be executed after submitting the form. In such a script file we may handle the variables from a particular form. These variables are passed to the script. Their names in the script corresponds to the names of input fields from the form. However, names of variables in the script have $ as prefix for the names from the form. In our example we may manipulate the following variables in our script:
- $name corresponds to the "name" input field from the form.
- $age corresponds to the "age" input field from the form.
- $preference corresponds to the "preference" input field from the form.
The action1.php3 script may simply dump the variables, as entered by a particular user:
To get the value entered in the form we first look that in the form what we have defined the method of the form.
If the method is "post" then to get the value we have to write
$HTTP_POST_VARS["field_name"]
and if it is "Get" then we will write $HTTP_GET_VARS["field_name"].
<?
/* this script will handle the variables passed from the action1.html file */
PRINT "<center>";
PRINT "Hi, $name.";
PRINT "<br><br>";
PRINT "You are $age years old and you like $preference.<br><br>";
PRINT "Thank you.";
PRINT "</center>";
?>
0 comments:
Post a Comment