Intro
This is one of the fastest (and raw) ways to process form information from a web page. We assume that you have a form setup with Dreamweaver or a html editor.
Getting started
In the $_POST global variable the results of a submitted from are stored (normally when someone clicks a submit button). Using the foreach() function we can generate something like this:
# Create an empty message variable.
$message="";
# The mail that we want to use to recevie the submitted information.
$to="info@mydomain.com";
# The E-Mail subject
$subject="This is a message from Page X";
# iterate the $_POST array
foreach ($_POST as $key=>$value) {
$message.=$key.": " . $value . "\n";
}
# Send message by e-mail.
$sm=mail($to, $subject, $message);
To get the most from this example name the fields in the form with common names like Name or Address dont use field1 or text2 since the format you will get looks like this: fieldname: fieldvalue.
Since the last example creates a formatted version of every part of the $_POST array oyu will see that even the submit button is included in the message. To avoid this and to avoid unwanted fields such as hidden fields that our code may need we can create some exeptions:
# Create an empty message variable.
$message="";
# The mail that we want to use to recevie the submitted information.
$to="info@mydomain.com";
# The E-Mail subject
$subject="This is a message from Page X";
# Fields to Avoid
$ex=array();
$ex[]="submit"; # Where submit is the name of the submit button.
# iterate the $_POST array
foreach ($_POST as $key=>$value) {
if (!in_array($key, $ex)) {
$message.=$key.": " . $value . "\n";
}
}
# Send message by e-mail.
$sm=mail($to, $subject, $message);
We can also add some error handling to be sure the e-mail was sent.
# Create an empty message variable.
$message="";
# The mail that we want to use to recevie the submitted information.
$to="info@mydomain.com";
# The E-Mail subject
$subject="This is a message from Page X";
# Fields to Avoid
$ex=array();
$ex[]="submit"; # Where submit is the name of the submit button.
# iterate the $_POST array
foreach ($_POST as $key=>$value) {
if (!in_array($key, $ex)) {
$message.=$key.": " . $value . "\n";
}
}
# Send message by e-mail.
$sm=mail($to, $subject, $message);
if (!$sm) {
print "Cannot send e-mail.";
}else{
print "E-Mail was sent.";
}
Now, ading more toys we can add some script if the mail() funcitons fails so we can store the message as a file.
# Create an empty message variable.
$message="";
# The mail that we want to use to recevie the submitted information.
$to="info@mydomain.com";
# The E-Mail subject
$subject="This is a message from Page X";
# Fields to Avoid
$ex=array();
$ex[]="submit"; # Where submit is the name of the submit button.
# iterate the $_POST array
foreach ($_POST as $key=>$value) {
if (!in_array($key, $ex)) {
$message.=$key.": " . $value . "\n";
}
}
# Send message by e-mail.
$sm=mail($to, $subject, $message);
if (!$sm) {
print "Cannot send e-mail.";
# Send info to a text file
$fp=fopen("file.txt", "a+"); # Change file to whatever filename you want. If the file is not there php will try to create it.
$fw=fwrite($fp, $message, strlen($message));
fclose($fp);
}else{
print "E-Mail was sent.";
}
Thats it... easy, nice and painless.