Forms

☰ Menu Content


Web sites use HTML forms to gather information from visitor.

Then PHP can be used to process that information.

Imagine we are an art supply store that sells brushes, paint, and erasers.

To accept orders from our customers we will need an HTML form.

Creating the HTML Form

First create an HTML form that will let our customers choose of what they like to purchase. 

Save this file as "order.html"

order.html Code:
<html>
    <head>
        <title>Order Form</title>
    </head>
    <body>
        <h4>MyanmarPhp Art Supply Order Form</h4>

            <form>
                <select>
                    <option>Paint</option>
                    <option>Brushes</option>
                    <option>Erasers</option>
                </select>
                Quantity: <input type="text" />
                <input type="submit" />
            </form>

    </body>
</html>
Display:

MyanmarPhp Art Supply Order Form

Quantity:

Next we must alter our HTML form to specify the PHP page we wish to send this information to. 

Also, we set the method to "post" and give name to the tags..

order.html Code:
<html>
    <head>
        <title>Order Form</title>
    </head>
    <body>
        <h4>MyanmarPhp Art Supply Order Form</h4>

            <form action="purchase.php" method="post">
                <select name="item">
                    <option>Paint</option>
                    <option>Brushes</option>
                    <option>Erasers</option>
                </select>
                Quantity: <input name="quantity" type="text" />
                <input type="submit" />
            </form>

    </body>
</html>

Now we will create the "purchase.php" file to precess HTML form information.

PHP Form Processor

The HTML form will send the customer's order to "purchase.php" file and it will extract the information by using PHP built in $_POST associative array.

You will learn about  $_POST  and  associate array later.

See the example below:-

purchase.php Code:
<html>
    <body>
        <?php
            $quantity = $_POST['quantity'];
            $item = $_POST['item'];

            echo "You ordered ". $quantity . " " . $item . ".<br />";
            echo "Thank you for ordering from Myanmarphp Art Supplies!";
        ?>
    </body>
</html>

As you probably noticed, the name in $_POST['name'] corresponds to the names that we specified in our HTML form.

Uploading the "order.html" and "purchase.php" files to a PHP enabled server and test them.

Say the customer selected the item "brushes" and specified a quantity of "6".

Then the following would be displayed on "purchase.php":

purchase.php Display:
You ordered 6 brushes.
Thank you for ordering from MyanmarPhp Art Supplies!

PHP & HTML Form Review

Let us step through the example to see what was going on.

  1. We first created an HTML form "order.html" that had two input fields specified, "item" and "quantity".
  2. We added two attributes to the form tag to point to "purchase.php" and set the method to "post".
  3. We had "purchase.php" get the information that was posted by setting new variables equal to the values in the $_POST associative array.
  4. We used the PHP echo function to output the customers order.

Note
For more detail please see Php Manual at http://www.php.net


Php Tutorial Content (menu)