+ Almost all quality improvement comes via simplification of design, manufacturing... layout, processes, and procedures.

July 30, 2010, 4:52 pm

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

Code (html)
  1. <form action="process.php" method="post">
  2. Name:     
  3. <input name="Name" />
  4. Email:     
  5. <input name="Email" />
  6. Address:     
  7. <input name="Address" />
  8. <input name="Address" type="submit" />
  9. </form>

Now to process it, first we will import all variables and send them through a loop

Code (php)
  1.  
  2.  
  3. @import_request_variables("gpc");     
  4.  
  5. $server = $_SERVER['HTTP_HOST'];     
  6.  
  7. foreach($_POST as $key => $data) {
  8.   if($key != ‘required’) {
  9.       if($data !=”) {
  10.           $message .= ‘<strong>’.$key.’</strong>: ‘.$data.”;
  11.       }
  12.   }
  13. }

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.

Code (php)
  1.  
  2.  
  3. echo ‘
  4. <p style="background-color: #f2fced; margin: 20px; border: #cccccc 1px solid; padding: 20px"> </p>
  5.  
  6. <h4>Your forum Has been sucessfully sent with the following information</h4>
  7. ‘.$message.’   
  8.  
  9. ‘;     
  10.  
  11. $m = new Mail(); // create the mail
  12. $m->From( "email@server.com" );
  13. $m->To( "email@server.com"  );
  14. $m->Subject( ‘New forum submission from  ‘.$server.’ Contact Page’ );
  15. $m->Body( $message);
  16. $m->Priority(4);     
  17.  
  18. $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)

2 Comments »

  1. 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

  2. hola
    I don’t agree with what you said really….
    please explain in detail a bit more for me :D

    cheers

    Comment by zodiaclove — October 10, 2008 @ 9:33 pm

RSS feed for comments on this post. | Trackback

Leave a comment

XHTML ( You can use these tags): <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .

*
To prove that you're not a bot, enter this code
Anti-Spam Image