Hardware Reference
In-Depth Information
Telnet into your web server
on port 80 and request the
file again from the command line. This
time, type the POST request as shown
here.
Try It
POST /age_checker.php HTTP/1.0
Host: example.com
Connection: Close
Content-Type: application/x-www-form-urlencoded
Content-length: 16
8 The content length is the length of this string,
plus a linefeed. If your name is longer than three
letters, or your age is greater or fewer than two
digits, change the content length to match.
You'll get the same response as you did
using GET, but now you can put all the
parameters in one place, at the end.
name=tom&age=14
Now make a couple changes to
the script. First, wrap the section that
checks name and age in another if
statement, like so (new lines are shown
in blue).
8
if (isset($name) && isset($age) ) {
if ($age < 21) {
echo "<p> $name, You're not old enough to drink.</p>\n";
} else {
echo "<p> Hi $name. You're old enough to have a drink, but do ";
echo "so responsibly.</p>\n";
}
}
?>
Add the following HTML to the age_
checker.php file after the closing PHP
tag.
<html>
<body>
<form action="age_checker.php" method="post"
enctype="application/x-www-form-urlencoded">
Name: <input type="text" name="name" /><br>
Age: <input type="age" name="age" />
<input type="submit" value="Submit" />
</form>
When you reload this script in the
browser, you will see a form as shown
in Figure 3-6. The if statement you
added makes sure the message about
age doesn't show up unless you've
entered values for name and age. And
the HTML form calls the same script
again when you submit your values,
using an HTTP POST request.
</body>
</html>
Figure 3-6
The PHP age-checker form.
 
Search WWH ::




Custom Search