Page 1 of 1

PHP - A dynamically created order form and receipt emailed to owner. Rate Topic: -----

#1 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9573
  • View blog
  • Posts: 36,255
  • Joined: 12-June 08

Posted 22 July 2008 - 12:16 PM

A little background on this large snippet/mini tutorial.

First off this was a major requirement for a side project I picked up. The stakeholder desired to have a chunk of inventory displayed (with pictures), a order form page that had a quantity box and the item description, and a receipt page that bundled up the information from the order form to have it redisplayed and emailed it to the stake holder. Their requirements not mine.

Second, this involves a MYSQL database table. If that is concerning in some how to you, you might want to stop.

Solution: I created two tables for the stakeholder. The first table was a decode table that contained a key/unique integer and a description. The second table was the actual item details table. This contained a unique/key lID (L-id), a product description, the location on the server where the image is stored, and the decode value for the product type. (The location of the image is irrelevant, but if someone wants to know how I implemented a massive item picture lookup through SQL - shoot me a PM.)

The solution is fairly straight forward. In the order form I query my table (using a join to get the decode string), and with each line's unquie lID I create an input text box name the lID value. Now when the user fills the information out and hit's submit, the data from those input text boxes are in the post $_REQUEST array of the receipt.php. The $key name is the unquie lID and the $value is the quantity they entered.

The stakeholder wanted the email to only contain the description of the items that the user entered a quantity next to, and not zero quantity items. Well that's puzzling because all I have are lID numbers as text box field names. I ended up going BACK to the database again with the same query and comparing the lID column from each line of the query against the $key value from the $_REQUEST array and snagging the description from there. This of course happens only after determining the value for quantity is not nothing. Everything was tossed in a large $message variable which is passed as the email's body message.

Places I am going to improve it above and beyond the stakeholder's initial needs.
1. insert the order back into another table for record keeping.
2. add a new column to the details table of 'sItemName' (varchar 30) and use that instead of the item description. I am unsure right now if the stakeholder has an item name in mind, but it would cut down on some of the over head.

<!-- orderform.php -->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">  
 <head>
 <title>PHP Test</title>
 
 </head>
 <body>

<?php
//-- 1.0 Database connection
	// Connecting, selecting database
	$link = mysql_connect('localhost', '<username', '<user password>')
		or die('Could not connect: ' . mysql_error());
	// echo '<p>Connected successfully</p>';
	mysql_select_db('<database name>') or die('Could not select database');
		
//-- 2.0 SQL select statement
	$query = 'SELECT 
	P.lID
	,P.sDescription
	,D.sValue
	FROM details as P
	LEFT JOIN decode as D
	ON D.tType = P.tType;';
	$result = mysql_query($query) or die('Query failed: ' . mysql_error());

//-- 3.0  Setting up the form
	echo '<form method="post" action="receipt.php">';
//-- 4.0 Basic one shot input boxes for the user name and email
	echo "Name: <input type='type' name='buyername'> <br>";
	echo "Email: <input type='type' name='email'> <br> <br> <br>";
//-- 4.1 Column name		
	echo "Quantity: <br>";
//-- 5.0 Create a inputbox with the name of the unique item key from the database
	while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) 
	{
		echo "<input type='text' name={$line['lID']}>";
		echo " : {$line['sDescription']}<br>";
	}
//-- 6.0 Setup the submit button		
	echo "<input type='submit' value='Send order'>";
	echo "</form>";
//-- 7.0 Close up results
	// Free resultset
	mysql_free_result($result);
		
	// Closing connection
	mysql_close($link);
?>
</body>
</html>


<!-- receipt.php -->

<?php
//-- 1.0 Database connection
	// Connecting, selecting database
	$link = mysql_connect('localhost', '<username', '<user password>')
		or die('Could not connect: ' . mysql_error());
	// echo '<p>Connected successfully</p>';
	mysql_select_db('<database name>') or die('Could not select database');
		
//-- 2.0 SQL select statement
	$query = 'SELECT 
	P.lID
	,P.sDescription
	,D.sValue
	FROM details as P
	LEFT JOIN decode as D
	ON D.tType = P.tType;';
	$result = mysql_query($query) or die('Query failed: ' . mysql_error());
//-- 3.0 From the order.php’s post.. there is one unique field for buyer name
	$buyer = $_REQUEST['buyername'];
//-- 3.1 Show it to the user
	echo "Buyer name: $buyer <br>";
//-- 3.2 add it to the email going to ourselfs
	$message = "Buyer: $buyer \n";
//--4.0 	From the order.php’s post.. there is one unique field for email
	echo "Buyer email: {$_REQUEST['email']} <br>";
//-- 4.1 Save it for later
	$email = "{$_REQUEST['email']}";
//-- 4.2 Make it part of the email to our self
	$message = $message . "Email: $email \n";
//-- 5.0 Get the current date time of the user	
	$orderDate = date('l jS \of F Y h:i:s A');
//-- 5.1 Save the current date time to the email to ourselves
	$message = $message . "Order date: $orderDate \n\n";
//-- 5.2 show the order date time
	echo "Order date: $orderDate <br>";
//-- 6.0 Random formatting
	echo "---------------------------------------------- <br>";
	echo "Quantity - Item <br>";
	echo "---------------- <br>";
//-- 7.0 For each item in the post from the order.php put the values to a key/val list pair.
	while(list($key, $val) = each($_REQUEST))
	{
//--7.1 Make sure we are retreading the same ground with the buyername an email.
		if (strcmp($key, "buyername")== 0 || strcmp($key, "email") == 0)
		{
			//nothing
		}
		else
		{
//-- 7.2 if the value for the input box is blank, ie they did not put a value, do not show it.
			if(strcmp(trim($val), "")!=0)
			{
//-- 7.3.0 To simplify the email to ourselves find the line who’s unique item $key id matches the 
//--		  id column in our sql statement.  We are doing this to dump the item description into the email
//--		  versus some non-human-readable id number.
				while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) 
				{
//-- 7.3.1 If the key and the id value match..
					if(strcmp($key, $line['lID'])== 0)
					{
						$quantity = trim($val);
//-- 7.3.2 Print out the quantity entered from the order.phph and the description from the database
						echo "$quantity - {$line['sDescription']} <br>";
//-- 7.3.3 Add that to our email to ourself
						$message = $message . "$quantity - {$line['sDescription']}  \n";
//-- 7.3.4 End the while statement to try and save on time.
						break;
					}
				}
			}
		}
	}
//-- 7.4 Email the dynamically built message string that now contains the user’s email, name,
//--		 order date and time, quantity, and description of only the items that have a quantity.	
 mail( "<email address>", "<subject>", $message, "From: $email" );

//-- 7.5 Thank you that displays after all the above has been processed and spit out
echo "<br>Thank you for ordering.  One of our representatives will get in touch with you with the price quote";
	
?>

This post has been edited by modi123_1: 28 July 2008 - 09:11 AM


Is This A Good Question/Topic? 1
  • +

Replies To: PHP - A dynamically created order form and receipt emailed to owner.

#2 migz3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-January 09

Posted 16 January 2009 - 01:36 PM

Thank you for sharing this.
Was This Post Helpful? 0
  • +
  • -

#3 s0s0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-November 14

Posted 21 November 2014 - 01:00 PM

please i want SQL FILE :(
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9573
  • View blog
  • Posts: 36,255
  • Joined: 12-June 08

Posted 21 November 2014 - 01:05 PM

Life is full of things I want to.

As for the tutorial - that part is something you would have to tailor for your needs.
Was This Post Helpful? 0
  • +
  • -

#5 s0s0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-November 14

Posted 21 November 2014 - 01:09 PM

I am new to learn and did not know his work database, because I did not understand what are the tables and content
SELECT
P.lID
,P.sDescription
,D.sValue
FROM details as P
LEFT JOIN decode as D
ON D.tType = P.tType;

i know like -> SELETC * FROM name etc. ....If do not like to help us anyway thanks :)
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9573
  • View blog
  • Posts: 36,255
  • Joined: 12-June 08

Posted 21 November 2014 - 01:11 PM

There must be a translation error - I am not getting what you are asking.
Was This Post Helpful? 0
  • +
  • -

#7 s0s0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-November 14

Posted 21 November 2014 - 01:15 PM

You are here to teach the novice professional must provide them with everything, even learn ..
I am thanking you
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9573
  • View blog
  • Posts: 36,255
  • Joined: 12-June 08

Posted 21 November 2014 - 01:17 PM

Quote

I am thanking you

You are quite welcomed!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1