Page 1 of 1

Record Form Data?

Posted: Sat Aug 28, 2010 4:12 pm
by lakshkhamesra
Hello PHP experts.
Please help me on this.
I have created a form at http://lakshwebdesign.com/clients/onev8 ... quote.html
How do I add these 3 PHP functionalities to it:-

1) On submit, all the data be sent to an email address.
2) Also, record it in database.
3) Record date on which the form is submitted.

I have knowledge of PHP and I can understand the codes somewhat so I am not a complete beginner of PHP.

Any help will be highly appreciated.

Thanks in advance.

Re: Record Form Data?

Posted: Sat Aug 28, 2010 7:56 pm
by Jonah Bron

Re: Record Form Data?

Posted: Sat Aug 28, 2010 7:56 pm
by MindOverBody
Hm, to send email with form data, there is premade PHP function called mail()
Here is description; or for more thoroughly info visit this: Send mail

Code: Select all

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
So you only need to get form data with $_POST for every form element, make form message string body of it, and in final send it with this function.

You did not provided much info about database table where you want to store form data, so i will only give you basic instructions how to do that.
First of all, you need to be connected to database. If you do not know how to connect to database, i recommend you to visit this link, and find out how to make it possible. There is awesome tutorial waiting for you. ;)

Now i can asume that you are connected to database, so what we need is to make our query string which will tell to database engine what to do. You need to get all of form post data with $_POST which is mentioned earlier. And put it in the right place in our query. Query you need to do is INSERT type query which will make new row in table and populate it with given data. So query should look something like this:

Code: Select all

"INSERT INTO table_name ( column_names ) VALUES ( values )"
For more info about insert query please read this: Insert Data Into a Database Table.

Third thing you asked, about submission date, is very simple to achive. Just make "submission_date" column in your table, and in your insert query place date value with time(), which will make unix timestamp, which can be easily formated in date/time format you want when needed.

I hope this will help you, if not, shout :)
Bojan

Re: Record Form Data?

Posted: Sat Aug 28, 2010 8:46 pm
by devarishi
I have done those tasks in the web applications I created.

First, form data is collected when it is submitted.

Secondly, the collected data is stored in a database / table (MS Access I used).

Thirdly, the same data is emailed to the concerned team members.

I give you the kind of PHP Script I wrote for this purpose. You modify it according to your requirement.


For Log Date & Time you can use something like this:

Code: Select all

$logDateTime	=	date('j-F-Y - H:i A');


If you are using MS-Access then this will do:

Code: Select all

#	Connecting to the Database and Adding the POSTed data to it...

	$db = 'MyDB.mdb';
	$conn = new COM('ADODB.Connection');
	$conn->Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$db;3");

	$sql = "insert into TableName values" . 
		"('" . $ProductName .  "','" . 
		$QtyOrdered . "','" .
		$logDateTime . "');";

	$rs = $conn->Execute($sql);

	$conn->close();
Modify the $sql statement according to your requirement.

The following script can be used for sending emails in Plain Text as well as in HTML format:

Code: Select all

<?php

$message = "
	<html>
	<body>

	<p>This is an auto-generated email.</p>

	</body>
	</html>	

	";

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: Service <service@company.com>;" . "\r\n";
$headers .= 'Cc: Others@company.com' . "\r\n";


$to 	= "people@company.com,managers@company.com";

$status = @mail($to,"Test Email", $message, $headers);



if ($status) {
	echo "<center><img src='success.jpg' border=0 title='Email Alert Sent' alt='Email Image'></center>";
	echo "<center><b>Note:</b> Do Not Hit the F5 Key Nor Refresh This Web Page</center>";
}

else {

	echo "<center><img src='error.jpg' border=0 title='Email Sent' alt='Sending Emails Failed!'></center>";
	echo "<center><b>Note:</b><br />There was an unexpected error while sending email alert.<br /> Do Not Hit the F5 Key Nor Refresh This Web Page</center>";

}


?>

Replace the contents of the variable $message with the contents you want to be emailed. Some hint:

For plain text format:

Code: Select all

$message = "";

$message .= "Product Name: " . $_POST["ProductName"]; 
$message .= "\nQuantity Ordered: " . $_POST["QtyOrdered"]; 

For HTML format:

Code: Select all

$message = "";

$message .= "Product Name: " . $_POST["ProductName"]; 
$message .= "<br />Quantity Ordered: " . $_POST["QtyOrdered"]; 



I suggest you to first store all the values / data submitted and collect them using $_POST or $_GET or $_REQUEST methods. Store the data in respective variables and then use them for storing values in the database / table and for packing them all together in $emailMsgBody (whatever you name it) for emailing them.

Note: The scripts I have given above works fine as I have written them and used in different web applications. Modify them according your requirements and post any error or unexpected output you get.