Creating a Form

All you need to create a form is given in HTML. There are two main ways of using HTML to layout a form, but the workings are the same.

Form Elements

These are the basic HTML tage needed to create a form:
1. <form> and <form> to start and end the form
2. <form method="post" action="process.php"> is an example of using the form tag - this would cause the form to call the program process.php to process any data entered in it
3. <input type="text" size="20" name="form_text"> for a text input field
4. <textarea name="form_textarea" rows="5" cols="20"> and then any default text in the textbox and </textarea> to end the text box
5. Radio buttons and selection boxes can also be used - see the going further box on the right if you want to use these
6. <input type="submit" value="Submit button"> for the button used by the user to submit the data in the form to be processed

Putting the form on the screen

The two ways used to layout these elements are:
A. Table - using table row tags and table element tags to place the elements on the page
B. CSS - using a sytle sheet to create layout elements that can be used to position the form elements.
There is some debate as to which is best to use, but either will work perfectly fine if you're not fussed about exact standards. Here'a a simple example of an e-mail form using both. First, using a table

<form method="post" action="process.php">
<table>
    <tr>
        <td>
            Enter your name:
        </td>
        <td>
            <input type="text" size="20" name="form_name">
        </td>
    </tr>
    <tr>
        <td>
            Enter your e-mail address:
        </td>
        <td>
            <input type="text" size="20" name="form_email">
        </td>
    </tr>
    <tr>
        <td>
            Enter your comment:
        </td>
        <td>
            <textarea name="form_textarea" rows="5" cols="20"> Enter your comments... </textarea>
        </td>
    </tr>
</table>
</form>


and now using a style sheet

Next article: Functions

Processing a form

There are 2 techniques to learn to procesa a form:
1. Use a hidden field in the form to check to see whether a form has posted. Then your PHP program can check to see if the form needs displaying or processing.
2. Use $_POST['form_variable'] to get the value of a variable entered in the form.

The general form a program to display and process a form is therefore as follows:

If hidden field has no value set
    display the form
else if hidden field has a value
    process the form, using $_POST['variable']


Example (without layout elements)

Here's an example of a PHP program that displays and processes a form. It's a simple program to give you the idea of wht to do. A more detailled, more realtic example is given in the more detail section on the right.

Form:

<input type="text" size="20" name="form_name">
<input type="text" size="20" name="form_address">
<input type="text" size="20" name="form_postcode">
<input type="submit" value="Go!">
<input type="hidden" name="form_posted" value="posted">


PHP program:

<?php
if (!array_key_exists('form_posted',$_POST)) {
Form has not posted because form_posted has no value
?> <input type="text" size="20" name="form_name">
<input type="text" size="20" name="form_address">
<input type="text" size="20" name="form_postcode">
<input type="submit" value="Go!">
<input type="hidden" name="form_posted" value="posted">
<?php
} else {
form_posted has got a value, so process the form
echo "Your name is "; echo $_POST['form_name'];
echo "Your address is "; echo $_POST['form_address'];
echo "Your postcode is "; echo $_POST['form_postcode'];
}


Next article: Functions

Previous

The previous article tells you how to use MySQL in PHP.

Next

The next screen tells you how to use functions in PHP.
The tuorial tell you how to create functions, pass parameters into a function and use them and how to return variables back from a function.

External Links

Summary

  • Create the layout of the form using the appropriate HTML tage
  • Process the form using PHP
  • Use $_POST or $_GET variables to access the data entered in the form
  • Use hidden values in the form to process the form posting