Dynamic PHP Email Forms without massive amounts of code
May 19, 2007
Ever had a HUGE form that just emails basic information and just thought to yourself, damn there has to be an easier way to do this! I’ve got a solution that will work, basicly what it does is takes the form loops all the input fields and sends them through email (or proccess them any way you would like) I’ll go over the basics here.
First we need a form Lets start with something simple
-
-
<form action="process.php" method="post">
-
Name:
-
<input name="Name" />
-
-
Email:
-
<input name="Email" />
-
-
Address:
-
<input name="Address" />
-
<input name="Address" type="submit" />
-
</form>
Now to process it, first we will import all variables and send them through a loop
-
-
-
@import_request_variables("gpc");
-
-
$server = $_SERVER['HTTP_HOST'];
-
-
foreach($_POST as $key => $data) {
-
if($key != ‘required’) {
-
if($data !=”) {
-
$message .= ‘<strong>’.$key.’</strong>: ‘.$data.”;
-
}
-
}
-
}
Now you can display your information if you wish or go further to send it with an email, im using a email class i built which you can download here.
-
-
-
echo ‘
-
<p style="background-color: #f2fced; margin: 20px; border: #cccccc 1px solid; padding: 20px"> </p>
-
-
<h4>Your forum Has been sucessfully sent with the following information</h4>
-
‘.$message.’
-
-
‘;
-
-
$m = new Mail(); // create the mail
-
$m->From( "email@server.com" );
-
$m->To( "email@server.com" );
-
$m->Subject( ‘New forum submission from ‘.$server.’ Contact Page’ );
-
$m->Body( $message);
-
$m->Priority(4);
-
-
$m->Send(); // send the mail
This method is very easy for sending simple forms where serverside validation is not needed. Check it out play with it and post if you have any questions ill be happy to help!
Posted by Anthony under PHP | Comments (2)
Does this actually keep me off of spam lists? Its the responsibility of the dev to make sure that his customers do not recieve massive amounts of spam.
Comment by alonzo — October 10, 2007 @ 5:55 pm
hola
I don’t agree with what you said really….
please explain in detail a bit more for me
cheers
Comment by zodiaclove — October 10, 2008 @ 9:33 pm