Processing forms using PHP
You can process HTML forms using these two powerful PHP
functions, $_REQUEST[] and $_POST[].
$_POST[] retreives value passed through form object.
$_REQUEST retrieves values from url passed variable value or value passed through form object.
Take a look at the following example:
<form name="userForm" method="post" action="userForm.php">
Enter a user name: <input type="text" name="userName" size="20">
Enter a password: <input type="password" name="password" size="20">
<inpute type="submit" name="submit" value="Send">
</form>
| Result
|
We just created HTML form and tell the browser to
process the form using the file "userForm.php".
The following is userForm.php file that writes the values
from the form.
<html>
<head>
<title>Process form Info</title>
</head>
<body>
<?php
print "You typed username: $_POST[userName]<br>";
print "and the password: $_REQUEST[password]";
?>
</body>
</html>
|
Type something in the text boxes and hit submit button to see the result
of this example. What you will see is the result of this code.
We use both $_POST[] & $_REQUEST[] for illustration.
|
PHP HTML Form Processing Example 2
You can store value retrieved from form into variables in order to use
these value whatever way you want. Take a look at this example which is
little bit more complex then previous one. It uses combination of checkbox, radio buttons, text fields, and text box:
<html>
<head>
<title>Form Example 2</title>
</head>
<body>
<p><b>This example process basic form elements</b>
<form method="POST" action="formProcess.php">
<p>Your name: <input type="text" name="Name" size="20"><br>
Status: <input type="radio" value="Customer" name="status">Customer
<input type="radio" name="status" value="Visitor">Visitor<br>
Do you own any of these trucks:<br>
<input type="checkbox" name="truck[]" value="Land Cruiser">Land Cruiser<br>
<input type="checkbox" name="truck[]" value="Sequoia">Sequoia<br>
<input TYPE="checkbox" name="truck[]" value="4Runner">4Runner<br>
<input TYPE="checkbox" name="truck[]" value="Highlander">Highlander<br>
<input TYPE="checkbox" name="truck[]" value="Tundra Access Cab">Tundra Access Cab<br>
Car of Choice:<select size="1" name="car">
<option value="MR2 Spyder">MR2 Spyder</option>
<option value="Celica">Celica</option>
<option value="Matrix">Matrix</option>
<option value="Avalon">Avalon</option>
<option value="Camry">Camry</option>
<option value="Corolla">Corolla</option>
<option value="Echo">Echo</option>
<option value="Prius">Prius</option>
<option value="RAV4 EV">RAV4 EV</option>
</select><br>
Enter some general comments about what you think about Toyota cars:<br>
<textarea rows="5" name="comment" cols="50"></textarea><br>
<align="center"><input type="submit" value="Submit" name="submit"><br>
</form>
</body>
</html>
|
Code for formProcess.php
<html>
<head>
<title>Result of your information</title>
</head>
<body>
<?php
$name=$_POST["Name"];
$status=$_POST["status"];
$car=$_POST["car"];
$comments=$_POST["comment"];
$truck=$_POST["truck"];
print "Your name: <b>$name</b><br>";
print "Status: <b>$status</b><br>";
print "Your favourite car is: <b>$car</b><br>";
print "You currently own these trucks:<br>";
foreach($truck as $t) {
echo "<b>".$t."</b><br />";
}
print "Your comments about Toyota products:<b>$comments";
?>
</b>
</body>
</html>
|
|
This example process basic html form elements by php script
|
To test this form, type some information and click the submit button.
|