Guest User

Peter Ehrlich

a guest
Dec 17th, 2009
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.55 KB | None | 0 0
  1. <?php
  2. //Peter Ehrlich - form submission handling
  3. //Last updated 12-17-09
  4.  
  5. // error_reporting(E_ALL); //this is for bugtesting purposes
  6.  
  7. // parameters for sending the email
  8. $EmailFrom = "[email protected]";
  9. $EmailTo = "[email protected]"; //you can use a comma seperated list here btw
  10. $subject = "Your Email Subject";
  11.  
  12. // parameters for database connection
  13. $dbHost = "localhost"; //or whatever else, if you have an external db
  14. $dbName = "some database";
  15. $dbPass = 'some passwd';
  16.  
  17. //initialize things.
  18. $validated=true;
  19. $vals = Array();
  20.  
  21. //echo "Working..."; //uncomment this to makes sure things are happening
  22.  
  23. //these help with DRY form validation.  A valid field here exists with some length > 0.
  24. function process($fieldName){
  25.     global $vals, $validated;  //don't use local copies.
  26.     if ((isset($_POST[$fieldName])) && (strlen(trim($_POST[$fieldName])) > 0)) {
  27.         $vals[$fieldName] = stripslashes(strip_tags($_POST[$fieldName]));  //strip code injection
  28.     } else {$validated=false; echo "VAL ERROR:".$vals[$fieldName];  $vals[$fieldName]  = 'null string';}
  29.     //^^ if the field is invalid
  30. }
  31.  
  32. function processMany($fieldNames){
  33.     foreach ($fieldNames as $name) {
  34.         process($name);
  35.     }
  36. }
  37.  
  38. function processOptionalField($fieldName){
  39.     global $vals, $validated;
  40.     if ((isset($_POST[$fieldName])) && (strlen(trim($_POST[$fieldName])) > 0)) {
  41.         $vals[$fieldName] = stripslashes(strip_tags($_POST[$fieldName]));
  42.     } else {$vals[$fieldName]  = 'blank';}
  43. }
  44.  
  45. //here we process an example form.  Demonstrates how to use the code to specify
  46. // the order of the fields sent out in the email.
  47. processMany(array('name','agency_name','address1'));
  48. processOptionalField('address2');
  49. processMany(array('City','state','zip','phone','email'));
  50.  
  51.  
  52. // prepare outgoing email:
  53. $body = "<table>\n";
  54.  
  55. foreach ($vals as $name => $value ){
  56.     $body .= "<tr><td>".$name."</td><td>".$value."</tr>\n";
  57. }
  58. $body .= "</table>";
  59.  
  60. // echo $body; //use this to test what was just made, as well as ensure that
  61. // all the values were recieved nicely.
  62.  
  63. // validate
  64. if (!$validated) {
  65.     echo "<br>error in validation :-(";  
  66.     //^^ you probably want a more professional error message here
  67.   print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html#validation\">";
  68.     //^^ comment this out when using ajax, or to view the above debugging output
  69.   exit;
  70. }
  71.  
  72. // Connect, select database
  73. $link = mysql_connect($dbhost, $dbName, $dbPass)    
  74.     or die('Could not connect: ' . mysql_error());
  75. echo 'Connected successfully';
  76. mysql_select_db('db_table_name') or die('Could not select database');
  77.  
  78. //when using a phone number, this will reduce it to an integer.
  79. $pattern = '/[^0-9]*/';
  80. $vals['number'] = preg_replace($pattern,'', $vals['number']);
  81. //^^make sure this refers to the correct field of course
  82.  
  83. //TODO: jQuery and php max length.... textareas limit to 1000, etcetc
  84. // (don't allow too long input)
  85.  
  86. //php is nice because it parses the $ within the string!
  87. $query = "INSERT INTO form_submissions VALUES ('$vals[name]','$vals[agency_name]'".
  88.     ",'$vals[address1]','$vals[address2]','$vals[City]','$vals[state]','$vals[zip]',".
  89.     "'$vals[phone]','$vals[email]')";
  90.  
  91. echo "<br><br><i>".$query."</i>";  //degbugging to show what the query is.
  92. mysql_query($query); //add the record to the database
  93.  
  94. mysql_close();
  95.  
  96. //although you'll probably be setting up the db w/ phpMyAdmin, its good
  97. // to save the creation code for reference, if nothing else.  It makes
  98. // it easier to change things later.
  99.  
  100. /* temp code: mysql table creation:
  101. CREATE TABLE  `bostonel_compliance`.`form_submissions` (
  102. `name` VARCHAR( 32 ) NOT NULL ,
  103. `agency_name` VARCHAR( 64 ) NOT NULL ,
  104. `address1` VARCHAR( 64 ) NOT NULL ,
  105. `address2` VARCHAR( 64 ) NOT NULL ,
  106. `City` VARCHAR( 32 ) NOT NULL ,
  107. `state` VARCHAR( 16 ) NOT NULL ,
  108. `zip` VARCHAR( 9 ) NOT NULL ,
  109. `phone` VARCHAR( 32 ) NOT NULL ,
  110. `email` VARCHAR( 64 ) NOT NULL
  111. ) ENGINE = MYISAM
  112. */
  113.  
  114. //setup the sending of the email:
  115. //for the sake of trivia: you don't need this for plain text (no html) emails.  
  116. // Its good to have it though!
  117. $headers  = 'MIME-Version: 1.0' . "\r\n";
  118. $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  119.  
  120. $headers .= "From: $EmailFrom";
  121.  
  122. $success = mail($EmailTo, $subject, $body, $headers);
  123.  
  124. // redirect to success page
  125. if ($success){
  126.     // echo $body;  //use this for debuggin
  127.     echo "success!";
  128.   print "<meta http-equiv=\"refresh\" content=\"0;URL=success.html\">";  
  129.     //^^ again, unnecessary when AJAX.
  130. }else{
  131.     echo "email fail!";
  132.   print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
  133. }
  134. ?>
  135.  
Advertisement
Add Comment
Please, Sign In to add comment