Technology news and Jobs arrow Information Technology News arrow Vamp the Linux LAMP through PHP part 2
Vamp the Linux LAMP through PHP part 2 E-mail
by David M Williams   
Monday, 01 October 2007
Handling web forms


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>
<head>
  <title>Form response</title>
</head>
<body>
  <h1>You typed:</h1>
<?php
  echo “Username: “ . $_REQUEST[“tUsername”] . “<br />”;
  echo “Password: “ . $_REQUEST[“tPassword”] . “<br />”;
?>
</body>
</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.

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.

  • Firstly, learn more about HTML forms.
  • Secondly, learn more about PHP.
  • Thirdly, even when you can respond to user input there’s still something your pages don’t do and that’s save data to a back-end database, or display pre-stored data out of a database. This is where MySQL comes in, the “M” in LAMP. We’ll cover that with another easy-to-follow tutorial in time, but for the moment you can find other online guides to working with MySQL in PHP.

    Good luck!
  • Powered By Joomla Tags

    Please enable JavaScript in your browser to post your comment!



     
    < Next story in category   Previous story in the category >
    iTWire user statistics Visitors last 30 days
    694,279
    Subscribers 15,210
    #1 independent technology news advertise here
    •   *  
    • Search
    • AdvSeach
    • Login
    • Events
    • FreeStuff

    - Advertisement -

    Featured Whitepapers

    Follow iTWire on Twitter

    About iTWire

    iTWire is all about technology news, information, jobs and community for the IT and telecommunications industry professional. Subscribe to our free ICT daily newsletter