| | |
E-mail form data quick and easily with PHP
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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:
[php]
# 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);
[/php]
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:
[php]
# 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);
[/php]
We can also add some error handling to be sure the e-mail was sent.
[php]
# 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.";
}
[/php]
Now, ading more toys we can add some script if the mail() funcitons fails so we can store the message as a file.
[php]
# 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.";
}
[/php]
Thats it... easy, nice and painless.
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:
[php]
# 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);
[/php]
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:
[php]
# 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);
[/php]
We can also add some error handling to be sure the e-mail was sent.
[php]
# 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.";
}
[/php]
Now, ading more toys we can add some script if the mail() funcitons fails so we can store the message as a file.
[php]
# 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.";
}
[/php]
Thats it... easy, nice and painless.
Last edited by happygeek; Oct 28th, 2006 at 11:16 am. Reason: Formatting
•
•
Join Date: Jul 2005
Posts: 2
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by RamiroS
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.
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:
[php]
# 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);
[/php]
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:
[php]
# 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);
[/php]
We can also add some error handling to be sure the e-mail was sent.
[php]
# 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.";
}
[/php]
Now, ading more toys we can add some script if the mail() funcitons fails so we can store the message as a file.
[php]
# 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.";
}
[/php]
Thats it... easy, nice and painless.
•
•
Join Date: Aug 2005
Posts: 1
Reputation:
Solved Threads: 0
i will sound like an idiot but i am trying to post vars back to an email address from a html page ie.
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#88a8e8" text="#f0fdfd">
<select name="select">
<option>1 </option>
<option>2 </option>
<option>3 </option>
<option>4 </option>
<option>5 </option>
</select>
<textarea name="test" cols="40" rows="5"></textarea>
</p>
<p>
<input type="submit" name="Post" value="Submit Job" action="<?php mail ('mail addrress@somewhere.com','Subject : help',(vars select & test),"addrress@somewhere.com");
?>"
I would like to know how to access the vars from php to post via a button , any help would be greatly appreshiated
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#88a8e8" text="#f0fdfd">
<select name="select">
<option>1 </option>
<option>2 </option>
<option>3 </option>
<option>4 </option>
<option>5 </option>
</select>
<textarea name="test" cols="40" rows="5"></textarea>
</p>
<p>
<input type="submit" name="Post" value="Submit Job" action="<?php mail ('mail addrress@somewhere.com','Subject : help',(vars select & test),"addrress@somewhere.com");
?>"
I would like to know how to access the vars from php to post via a button , any help would be greatly appreshiated
•
•
Join Date: Aug 2005
Posts: 1
Reputation:
Solved Threads: 0
Hi,
I was wondering if you could email the code for this email form to me. I also wasn't sure if you knew how I could also make it possible for this form to be sent to the person filling out the form as well?
I have been looking for someone to help me out. So I would greatly appreciate what you have to offer.
Ciao.
I was wondering if you could email the code for this email form to me. I also wasn't sure if you knew how I could also make it possible for this form to be sent to the person filling out the form as well?
I have been looking for someone to help me out. So I would greatly appreciate what you have to offer.
Ciao.
![]() |
Similar Threads
- Passing form data to default file of a directory (PHP)
- php mail (PHP)
- Form data & Back button (PHP)
- php mail form - need to redirect to new page (PHP)
- Code Snippet: E-mail form data quick and easily with PHP (PHP)
Other Threads in the PHP Forum
- Previous Thread: Help
- Next Thread: folder searching
| Thread Tools | Search this Thread |
apache api array beginner binary broken cache cakephp checkbox class cms code codingproblem cron curl customizableitems database date display dynamic echo email error errorlog file files filter folder form format forms forum function functions gc_maxlifetime global google headmethod host href htaccess html image include insert ip javascript joomla limit link login mail malfunctioning memmory memory menu mlm multiple mysql nodes oop parameter parsing paypal pdf php phpmysql problem query radio random recursion recursiveloop remote script search select server sessions sms snippet source space sql static survey syntax system table trouble tutorial up-to-date update upload url validator variable video web youtube





