Advertisement
gitlez

YA: One Page Contact Form

Apr 2nd, 2012
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.66 KB | None | 0 0
  1. <?php
  2. /* Paste in response to yahoo answers question */
  3.  
  4. $output = ''; // Will hold the HTML to display to the user, based on the request scenario.
  5. $HTML = Array( // $HTML Array to hold all the neccessary HTML
  6.     'form' => // The Form HTML
  7.         '<form method="post">
  8.             <p>Your name</p>
  9.             <p>
  10.                 <input type="text" name="cf_name">
  11.             </p>
  12.             <p>Your e-mail</p>
  13.             <p>
  14.                 <input type="text" name="cf_email">
  15.             </p>
  16.             <p>Subject</p>
  17.             <p>
  18.                 <input type="text" name="cf_subject">
  19.             </p>
  20.             <p>Message</p>
  21.             <p>
  22.                 <input type="text" name="cf_message">
  23.             </p>
  24.             <p>Who do you want to contact:</p>
  25.             <p>
  26.                 <select name="cf_address" size="1">
  27.                     <option></option>
  28.                     <option disabled>--Group--</option>
  29.                     <option value="email">  Option</option>
  30.                     <option value="email">  Option</option>
  31.                     <option disabled>--Group--</option>
  32.                     <option value="email">  Option</option>
  33.                     <option disabled>--Group--</option>
  34.                     <option value="email">  Option</option>
  35.                     <option value="email">  Option</option>
  36.                 </select>
  37.             </p>
  38.  
  39.             <input type="submit"value="Submit">
  40.             <input type="reset" value="Clear">
  41.         </form>
  42.     ',
  43.     'success' => // The HTML for the success message
  44.         '<h3>Thank You</h3>
  45.         <p>Your message has been received. <br> We thank you for taking the time to message us.</p>
  46.         <p>Someone will respond to your message as soon as possible.</p>
  47.     ',
  48.     'error' => // Then HTML message for an error occuring
  49.         '<h3>Internal Error</h3>
  50.         <p>We\'re sorry to report that there has been an internal server error.</p>
  51.         <p>Your message was not received. Please try again later.</p>
  52.         <p>Sorry for any incovenience this has caused.</p>
  53.     ');
  54.  
  55. if( $_SERVER['REQUEST_METHOD'] === 'POST' ){ // If the page is requested using the 'POST' method, the way in which your form requests the results page.
  56.     // You should have a function to check the inputs for "Email Injections". Google it!
  57.     // You should also be checking to see if any of the important fields are left blank.
  58.     $field_name = $_POST['cf_name'];
  59.     $field_email = $_POST['cf_email'];
  60.     $subject = $_POST['cf_subject'];
  61.     $field_message = $_POST['cf_message'];
  62.     $mail_to = $_POST['cf_address'];
  63.  
  64.     $body_message = 'From: '.$field_name."\n";
  65.     $body_message .= 'E-mail: '.$field_email."\n";
  66.     $body_message .= 'Message: '.$field_message;
  67.  
  68.     $headers = 'From: '.$cf_email."\r\n";
  69.     $headers .= 'Reply-To: '.$cf_email."\r\n";
  70.  
  71.     $mail_status = mail($mail_to, $subject, $body_message, $headers);
  72.  
  73.     if ($mail_status) {
  74.         $output = $HTML['success'];
  75.     } else {
  76.         $output = $HTML['error'];
  77.     }
  78. }else{ // The request is a GET request
  79.     $output = $HTML['form'];
  80. }
  81.  
  82. echo $output;
  83.  
  84.  
  85. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement