Ad

Saturday, April 26, 2008

Designing a Simple Order Form Application in PHP

Designing a Simple Order Form Application in PHP

Ok guys, for this post we’re going to create a simple yet complete order form page. Order forms are used on many sites to take customers order online. Order forms should have the capability to take orders from visitors regarding what items they want to purchase and store the information for further processing.

For this post’s example, we are going to create an order form for a Book Seller. The form will be designed to take order of five different items (books). Our order form application should be able to take order of five different items in any separate quantities tht user wants, it should also ask for shipping address and name of the customer. It should then store the information provided in a file along with the date and time order was placed. The application should also be able to take any number of orders and store them all linearly for further human processing.

For this, we need a front end of a HTML form to which the user would interact and supply information regarding their purchase. These information would then be sent to a PHP script that would show the order and save the same in a file.

Wait, did I say File? Yeah, PHP has a good support for file I/O on the server.

Below is the code for the HTML front-end page:


<html>
<head>
<title>My Book Store: Order Form</title>
</head>

<body>
<h1>My Book Store</h1>
<h2>Orders Form</h2>
<form name="form1" id="form1" method="post" action="takeorder.php">
<div align="center">
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr bgcolor="#CCCCCC">
<td height="19">NAME OF BOOK</td>
<td>QUANTITY</td>
</tr>
<tr>
<td height="19">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td width="202">Book on PHP</td>
<td width="298"><input name="q1" type="text" id="q1" value="0" size="5" />
</td>
</tr>
<tr>
<td>Book on MySQL</td>
<td><input name="q2" type="text" id="q2" value="0" size="5" /> </td>
</tr>
<tr>
<td>Book on ASP</td>
<td><input name="q3" type="text" id="q3" value="0" size="5" /> </td>
</tr>
<tr>
<td>Book on Web Development</td>
<td><input name="q4" type="text" id="q4" value="0" size="5" /> </td>
</tr>
<tr>
<td>Book on AJAX</td>
<td><input name="q5" type="text" id="q5" value="0" size="5" /> </td>
</tr>
<tr>
<td height="19">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr bgcolor="#CCCCCC">
<td>Personal Information</td>
<td>&nbsp;</td>
</tr>
<tr>
<td height="19">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Your Name</td>
<td><input name="name" type="text" id="name" size="40" /> </td>
</tr>
<tr>
<td>Shipping Address</td>
<td><input name="address" type="text" id="address" size="40" /> </td>
</tr>
</table>
</div>
<p align="center">
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
</body>
</html>


It has a form and several text boxes to take the quantity of different items, shipping address and name of the customer.

NOTE: Quantity text boxes have been given default values of ‘0’. And POST method is used to send data.

Now, below is the PHP script:

<html>
<head><title>Order Processed</title></head>
<body>
<h1>My Book Store</h1>
<h2>Order Successfully Processed</h2>
<?php
//have the POST data
$qty[0]=(int)$_POST['q1'];
$qty[1]=(int)$_POST['q2'];
$qty[2]=(int)$_POST['q3'];
$qty[3]=(int)$_POST['q4'];
$qty[4]=(int)$_POST['q5'];

//have the name and address
$customer_name=$_POST['name'];
$shipping_address=$_POST['address'];

//calculate total number of books
$total_qty=$qty[0]+$qty[1]+$qty[2]+$qty[3]+$qty[4];

//print out the order information
echo "<h3>".date("H:i A, jS F Y")."</h3>";
echo "<p>Total Number of Books Requested: ".$total_qty."</p>";
echo "Items will ship to: <br />";
echo "<b>".$customer_name."</b><br />";
echo $shipping_address."<br />";

//open file
$fp=fopen("orders.txt","a");

//create a string of data to be written to the file
//format the data using "\n" to move to a new line
//in the file
$data="---------ORDER START----------\n";
for($i=0;$i<count($qty);$i++)
$data.="Item ".$i.": ".$qty[$i]."\n";

$data.="\nName: ".$customer_name;
$data.="\nAddress: ".$shipping_address;
$data.="\n---------ORDER END----------\n\n\n";

//write th data to the opened file
fputs($fp,$data);

//close the file
fclose($fp);
?>
</body>
</html>

Look at this line

  $fp=fopen("orders.txt","a");

This function opens the file having the name provided in the first argument in the mode provided in the second argument. We’re using “Append” file mode which creates the file named (orders.txt) to start with (since it doesn’t exist). In the second or consecutive runs however as the file exists, new information is appended to the end of the file.

So, after two runs, the file (orders.txt) would contain the two separate order information as below:

---------ORDER START----------
Item 0: 2
Item 1: 5
Item 2: 5
Item 3: 10
Item 4: 10

Name: Test User
Address: Test Address, Test Street, Test Town
---------ORDER END----------


---------ORDER START----------
Item 0: 10
Item 1: 2
Item 2: 1
Item 3: 5
Item 4: 5

Name: Someone Testing
Address: From Somewhere
---------ORDER END---------

If you put these types of script on your site, you’d have to check for new orders every now and then by opening the orders.txt file. After the orders have been processed you may delete the file, so that only unprocessed orders remain in the file or you may do whatever you wish!

In the next post we’ll discuss about the file processing part that we’ve used here.

Related Articles:

No comments:

Post a Comment

You are free to comment anything, although you can comment as 'Anonymous' it is strongly recommended that you supply your name. Thank You.

Please don't use abusive language.