Advertisement
gitlez

YA: PHP Form Processing

Jul 13th, 2011
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.63 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.     Response to Yahoo Question: http://answers.yahoo.com/question/?qid=20110712113235AAkRVNc
  5.  
  6. */
  7.  
  8. // Set the Timezone to your local timezone Setting.
  9. // To get the appropriate time listing/setting visit:
  10. //  http://www.php.net/manual/en/timezones.php
  11. // Beware the the timezones are case sensitive
  12. date_default_timezone_set('America/New_York');
  13.  
  14. // Functions
  15. function postVars(){ // Will bind all inputed values to variables with the same name
  16.     $a = func_get_args();
  17.     $c = func_num_args();
  18.     $m = get_magic_quotes_gpc();
  19.     for($i=0;$i<$c;++$i){
  20.         Global ${$a[$i]};
  21.         $v = trim($_POST[$a[$i]]);
  22.         ${$a[$i]} = ($m)? stripslashes($v) : $v;
  23.     }
  24. }
  25. function requiredEntries(){
  26.     $a = func_get_args();
  27.     $c = func_num_args();
  28.     $e = Array();
  29.     for($i=0;$i<$c;++$i){
  30.         if(strlen(trim($_POST[$a[$i]])) === 0){
  31.             $e[] = ucwords($a[$i]) . ' is a required field.';
  32.         }
  33.     }
  34.     $c = count($e);
  35.     if($c > 0){
  36.         return 'The following error(s) were detected:<br />- ' . implode('<br />- ',$e) . '<br />Please correct before continueing. <a href="Javascript: history.go(-1);">Return to form</a>';
  37.     }else{
  38.         return true;
  39.     }
  40. }
  41.  
  42. // Check for required inputs
  43. $required = requiredEntries('Email','Salutation','FName','LName','Community','Address','Phone');
  44. if($required !== true){
  45.     die($required);
  46. }
  47.  
  48. // get posted data into local variables
  49. bindPostVars('Email','Salutation','FName','LName','Community','Address','Phone','Comment');
  50. $EmailFrom = 'maintenance@gebhartproperties.com'
  51. $successPage = 'success.php'; // The page to send the user to on a successful sending of the email
  52. $errorPage = 'errorPage.php'; // User will be sent to if the mail() function fails, not if the user inputs errored information.
  53.  
  54. // Variable Dependents
  55. if($Community === 'North' || $Community === 'East'){
  56.     $EmailTo = strtolower($Community) . 'maintenance@gebhartproperties.com';
  57.     $Subject = $Community . ' Maintenance Request';
  58. }else {
  59.     die('Invalid Community Input.  <a href="Javascript: history.go(-1);">Return to form</a>');
  60. }
  61.  
  62. // prepare email body text
  63. $body = '
  64. Email: ' . $Email . '
  65. Salutation: ' . $Salutation . '
  66. First Name: ' . $FName . '
  67. Last Name: ' . $LName . '
  68. Phone: ' . $Phone . '
  69. Community: ' . $Community . '
  70. Address:
  71. ' . $Address . '
  72. Comment:
  73. ' . $Comment . '
  74. Message Received: ' . date("H:i:s    F n, Y") . '
  75. ';
  76.  
  77. // send email & redirect to success page
  78. if (mail($EmailTo, $Subject, $Body, "From: Gebhart Maintenance <{$EmailFrom}>")){
  79.     header('Location: ' . $successPage);
  80. }else{
  81.     header('Location: ' . $errorPage);
  82. }
  83. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement