Skip to main content

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:

Popular posts from this blog

Fix For Toshiba Satellite "RTC Battery is Low" Error (with Pictures)

RTC Battery is Low Error on a Toshiba Satellite laptop "RTC Battery is Low..." An error message flashing while you try to boot your laptop is enough to panic many people. But worry not! "RTC Battery" stands for Real-Time Clock battery which almost all laptops and PCs have on their motherboard to power the clock and sometimes to also keep the CMOS settings from getting erased while the system is switched off.  It is not uncommon for these batteries to last for years before requiring a replacement as the clock consumes very less power. And contrary to what some people tell you - they are not rechargeable or getting charged while your computer or laptop is running. In this article, we'll learn everything about RTC batteries and how to fix the error on your Toshiba Satellite laptop. What is an RTC Battery? RTC or CMOS batteries are small coin-shaped lithium batteries with a 3-volts output. Most laptops use

The Best Way(s) to Comment out PHP/HTML Code

PHP supports various styles of comments. Please check the following example: <?php // Single line comment code (); # Single line Comment code2 (); /* Multi Line comment code(); The code inside doesn't run */ // /* This doesn NOT start a multi-line comment block /* Multi line comment block The following line still ends the multi-line comment block //*/ The " # " comment style, though, is rarely used. Do note, in the example, that anything (even a multi-block comment /* ) after a " // " or " # " is a comment, and /* */ around any single-line comment overrides it. This information will come in handy when we learn about some neat tricks next. Comment out PHP Code Blocks Check the following code <?php //* Toggle line if ( 1 ) {      // } else {      // } //*/ //* Toggle line if ( 2 ) {      // } else {      // } //*/ Now see how easy it is to toggle a part of PHP code by just removing or adding a single " / " from th

Generating XML Feeds (RSS, Atom) Using PHP

RSS/ATOM feeds are very common these days and almost all Content Management Systems (CMS) can generate it. But in the case when you want to generate it yourself or just want to learn how you can, read on! Both RSS and ATOM feeds are written in eXtensible Markup Language (XML) standard markup. Not just standard markups, you also need to be sure of what and how you put data in those markup elements (tags). For all this refer to the feed specifications of RSS and ATOM . XML itself is very strict and the standard specifications makes it even harder to generate valid feeds. And moreover, why re-invent the wheel when we can have it – ready-made. The solution I'm referring to here is, to use a third-party Library – Universal FeedWriter. FeedWriter is a PHP class written by Anis uddin Ahmad that can dramatically  ease-off feeds (both RSS and Atom) generation. You can download this library from  here . Every feed should have at least the following data: Feed title URL(of the webs