Creating the HTML Form
If you need a refresher on how to properly make an HTML form, check out the HTML Form Lesson before continuing on.
We first create an HTML form that will let our customer choose what they would like to purchase. This file should be saved as “order.html”
<html><body> <h4>Tizag 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>
Tizag Art Supply Order Form
Remember to review HTML Forms if you do not understand any of the above HTML code. 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”.
<html><body> <h4>Tizag Art Supply Order Form</h4> <form action="process.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 that our “order.html” is complete, let us continue on and create the “process.php” file which will process the HTML form information.
PHP Form Processor
We want to get the “item” and “quantity” inputs that we have specified in our HTML form. Using an associative array (this term is explained in the array lesson), we can get this information from the $_POST associative array.
The proper way to get this information would be to create two new variables, $item and $quantity and set them equal to the values that have been “posted”. The name of this file is “process.php”.
<html><body> <?php $quantity = $_POST['quantity']; $item = $_POST['item']; echo "You ordered ". $quantity . " " . $item . ".<br />"; echo "Thank you for ordering from Tizag Art Supplies!"; ?> </body></html>
As you probably noticed, the name in $_POST[‘name‘] corresponds to the name that we specified in our HTML form.
Now try uploading the “order.html” and “process.php” files to a PHP enabled server and test them out. If someone selected the item brushes and specified a quantity of 6, then the following would be displayed on “process.php”:
You ordered 6 brushes. Thank you for ordering from Tizag Art Supplies!
PHP & HTML Form Review
A lot of things were going on in this example. Let us step through it to be sure you understand what was going on.
- We first created an HTML form “order.html” that had two input fields specified, “item” and “quantity”.
- We added two attributes to the form tag to point to “process.php” and set the method to “post”.
- We had “process.php” get the information that was posted by setting new variables equal to the values in the $_POST associative array.
- We used the PHP echo function to output the customers order.
3,007 total views, 2 views today