A Working Form

We now have enough information to construct a working form, so here goes:

    <form action="someform.php" method="post">
    Name: <input type="text" name="Name" value="Jim" /><br />
    Password: <input type="password" name="Password" /><br />
    Age: <input type="text" name="Age" /><br />
    <input type="submit" />

That will submit three variables to someform.php: Name, Password, and Age. Form variables are given names using the name attribute—the names you use here will be used in the PHP script that receives the variables. The default value of a field can be set using the value attribute, which means that the Name text box will be set to Jim by default.

This new form is shown in Figure 9-3.

The Age field, which will presumably contain numbers like 18, 34, etc., is the same type as the Name field, which is likely to contain strings like “Bob,” “Sarah,” etc. HTML does not have any way to say “restrict this field to numbers only,” which means users can enter their age as “Elephant,” if they wish. Never trust input from users!

And now a more complicated form, using various other types:

    <form action="someform.php" method="get">
    Name: <input type="text" name="Name" value="Jim" /><br />
This time the form is more advanced—note the default value for the Name field
Figure 9-3. This time the form is more advanced—note the default value for the Name field
 Password: <input type="password" name="Password" maxlength="10" /><br /> Age range: <select name="Age"> <option ...

Get PHP in a Nutshell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.