Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. <?php echo $response; ?>
  2. <form id="contactForm" action="<?php the_permalink(); ?>" method="post">
  3. <p><input class="input" type="text" name="message_name" value="<?php echo esc_attr($_POST['message_name']); ?>" placeholder="First & Last Name"></p>
  4. <p><input class="input" type="text" name="message_text" value="<?php echo esc_attr($_POST['message_text']); ?>" placeholder="Company name"></p>
  5. <p><input class="input" type="text" name="message_email" value="<?php echo esc_attr($_POST['message_email']); ?>" placeholder="Email address"></p>
  6. <input type="hidden" name="submitted" value="1">
  7. <p><input type="submit" value="Send" /></p>
  8. </form>
  9.  
  10. <?php
  11. //response generation function
  12. $response = "";
  13.  
  14. //function to generate response
  15. function first_contact_form_generate_response($type, $message){
  16. global $response;
  17. if($type == "success") {
  18. $response = "<div class='text--green'>{$message}</div>";
  19. $_POST = array();
  20. } else {
  21. $response = "<div class='text--red'>{$message}</div>";
  22. }
  23. }
  24.  
  25. //response messages
  26. $missing_content = "Please supply all information.";
  27. $email_invalid = "Email Address Invalid.";
  28. $message_unsent = "Message was not sent. Try Again.";
  29. $message_sent = "Thank you! Your request has been sent.";
  30.  
  31. //user posted variables
  32. $name = $_POST['message_name'];
  33. $email = $_POST['message_email'];
  34. $message = 'Full name: ' . "rn" . $name . "rnrn" . 'Company name: ' . "rn" . $_POST['message_text'] . "rnrn" . 'Email address: ' . "rn" . $email;
  35.  
  36. //php mailer variables
  37. $to = "email@site.com";
  38. $subject = "Request by ".$email;
  39. $headers = 'From: '. $email . "rn" .
  40. 'Reply-To: ' . $email . "rn";
  41.  
  42. //validate email
  43. if ($_POST['submitted']) {
  44. if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
  45. first_contact_form_generate_response("error", $email_invalid);
  46. }
  47. else //email is valid
  48. {
  49. //validate presence of name and message
  50. if(empty($name) || empty($message)){
  51. first_contact_form_generate_response("error", $missing_content);
  52. }
  53. else //ready to go!
  54. {
  55. $sent = wp_mail($to, $subject, strip_tags($message), $headers);
  56. if($sent) first_contact_form_generate_response("success", $message_sent); //message sent!
  57. else first_contact_form_generate_response("error", $message_unsent); //message wasn't sent
  58. }
  59. }
  60. } else if ($_POST['submitted']) first_contact_form_generate_response("error", $missing_content);
  61. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement