A number of Australian employees of Hewlett-Packard are facing the loss of their jobs as the global computer giant looks to slash its worldwide workforce by up to 30,000.
read more
David M Williams
Monday, 01 October 2007 13:38
This leads us to actually making your web pages more useful. In standard HTML you can make web forms which prompt the user for input. PHP is able to act on the input using a special array which is defined for you.
Here’s a sample form. You should create it using any text editor and save it to your web space.
<html>
<head>
<title>Sample form</title>
</head>
<body>
<h1>Please enter your username and password to continue:</h1>
<form method=”post” action=”login.php”>
Username: <input type=”text” name=”tUsername” /><br />
Password: <input type=”password” name=”tPassword:” /><br />
<input type=”submit” />
</form>
</body>
</html>
This form displays two fields, which are named tUsername and tPassword. Both hold text strings. The form’s action is to move to page login.php when the user clicks the submit button. So, if this page were displayed in a web browser, the user would be able to enter a username and password and click submit. The web browser would then call the next page, login.php, and pass it the data which the user entered. It’s up to login.php to read these values and use them in some way and here’s where the magic happens.
When a PHP page is called as the target of a form, a special array is created called $_REQUEST. You can use the values in this array just as if it were an array you made yourself. Here’s a sample login.php page which shows back what the user typed in:
<html>This is a perfectly legal and valid PHP page. We don’t declare the $_REQUEST array anywhere; it is provided automatically by PHP. We don’t have to tell it that array elements exist with keys “tUsername” or “tPassword”; PHP does that by itself – and you can see right away that those keys are the names given to the form fields in the HTML page. If we had more form elements – be they checkboxes, radio buttons, selection lists, drop-down lists or anything else, these would also be automatically defined as elements in the array.
<head>
<title>Form response</title>
</head>
<body>
<h1>You typed:</h1>
<?php
echo “Username: “ . $_REQUEST[“tUsername”] . “<br />”;
echo “Password: “ . $_REQUEST[“tPassword”] . “<br />”;
?>
</body>
</html>
If you ran this example and gave a username of “tomthumb” and a password “jemima” then $_REQUEST[“tUsername”] would have the value “tomthumb” and $_REQUEST[“tPassword”] would have the value “jemima”. It really is that simple.
And that’s how to handle HTML forms in PHP. This gives you loads of power to make dynamic web pages which respond intelligently to what the user asks, or enters as responses to questions your web site asks.
With this knowledge, there’s nothing stopping you making your own PHP calculator, say. Or an expert system which asks the user questions to diagnose a problem and then gives its considered opinion. You can chain multiple pages of forms together to make a series of questions. In fact, as the PHP echo command lets your output your own HTML tags, you could dynamically generate entire HTML pages and forms on the fly. Mix this with conditional statements like if ... else and you can have your PHP pages produce different output – and different forms with different target pages when submitted – based on the flow of data.
From here the best thing I can do is to advocate further reading in three areas.
Think again. Most businesses only have PART of a DR plan - and this spells business disaster in the event of an IT disaster.
Download The Seven Sins of Disaster Recovery White Paper now and find out how you can prevent this happening to you.