PHP Programing language

adplus-dvertising
PHP FORMS
Previous Home Next

Using PHP Forms with HTML Forms

The PHP using with HTML File .it is time to apply the knowledge you have obtained thus far and put it to real use. The very common application of PHP is to have an HTML form gather information from a website's visitor and then use PHP to do process that information.

If you want to using PHP with HTML form then imagine we are a Stationary supply store that sells Pencil, Color, and Books. To gather order information from our prospective customers we will have to make a page with an HTML form to gather the customer's order.

Creating the HTML Form

We want to create a HTML form so, first create an HTML form that will let our customer and after choose what they would like to purchase. This file should be saved as "order.html"

<html><body>
<h1> BOOK Stationary Order Form</h1>
<form> 
<select> 
<option>Pencil</option>
<option>Color</option>
<option>Book</option>
</select>
Quantity: <input type="text" /> 
<input type="submit" />
</form>
</body></html>

Output:

BOOk Stationary 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>
<h1>BOOk Stationary Order Form</h1>
<form action="pro.php" method="post"> 
<select name="items"> 
<option>Pencil</option>
<option>Color</option>
<option>Book</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 "pro.php" file which will process the HTML form information.

What is PHP Form Processor

In HTML Form specified into the "item" and "quantity" inputs. and the using an associative array , we can get this information from the $_POST associative array.

So, proper way to get this information would be to creating in two new variables, First one is $item and another $quantity and set them equal to the values that have been "posted". The name of this file is "pro.php".

<html><body>
<?php
$quantity = $_POST ['quantity'];
$item = $_POST ['item'];
echo "You ordered ". $quantity . " " . $item . ".<br />";
echo "this is the best way for selling!";
?>
</body></html>

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

PHP & HTML Form Review

  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 "process.php" and set the method to "post".
  3. We had "process.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.
Previous Home Next