Advertisement
Guest User

Untitled

a guest
Jan 8th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.80 KB | None | 0 0
  1. app.controller('ContactController', function ($scope, $http) {
  2. $scope.result = 'hidden'
  3. $scope.resultMessage;
  4. $scope.formData; //formData is an object holding the name, email, subject, and message
  5. $scope.submitButtonDisabled = false;
  6. $scope.submitted = false;
  7. $scope.submit = function(contactform) {
  8. $scope.submitted = true;
  9. $scope.submitButtonDisabled = true;
  10. if (contactform.$valid) {
  11. var request = $http({
  12. method : 'POST',
  13. url : 'php/contact.php',
  14. data : $.param($scope.formData), //param method from jQuery
  15. headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
  16. });
  17. if (request.success) {
  18. console.log(request);
  19. $scope.submitButtonDisabled = false;
  20. $scope.result='bg-success';
  21. $scope.resultMessage = request.message;
  22. } else {
  23. $scope.submitButtonDisabled = true;
  24. $scope.resultMessage = request.message;
  25. //$scope.resultMessage = "Opps!... something went wrong. Please Contact OpenHouse directly to let them know of this error.";
  26. $scope.result='bg-danger';
  27. };
  28. //};
  29. } else {
  30. $scope.submitButtonDisabled = false;
  31. $scope.resultMessage = 'Failed <img src="http://www.chaosm.net/blog/wp-includes/images/smilies/icon_sad.gif" alt=":(" class="wp-smiley"> Please fill out all the fields.';
  32. $scope.result='bg-danger';
  33. }
  34. }
  35. });
  36.  
  37. <?php
  38.  
  39. require_once ("class.phpmailer.php"); // Include phpmailer class
  40. ini_set('display_errors', 'On');
  41. error_reporting(E_ALL | E_STRICT);
  42.  
  43. if (isset($_POST['inputFirstName']) && isset($_POST['inputLastName']) && isset($_POST['inputEmail']) && isset($_POST['inputPhone']) && isset($_POST['inputMessage'])) {
  44.  
  45. //check if any of the inputs are empty
  46. if (empty($_POST['inputFirstName']) || empty($_POST['inputLastName']) || empty($_POST['inputEmail']) || empty($_POST['inputPhone']) || empty($_POST['inputMessage'])) {
  47. $data = array('success' => false, 'message' => 'Please fill out the form completely.');
  48. echo json_encode($data);
  49. exit;
  50. }
  51.  
  52. $message=
  53. 'First Name: '.$_POST['inputFirstName'].'<br />
  54. Last Name: '.$_POST['inputLastName'].'<br />
  55. Phone: '.$_POST['inputPhone'].'<br />
  56. Email: '.$_POST['inputEmail'].'<br />
  57. Comments: '.$_POST['inputMessage'].'
  58. ';
  59.  
  60. $mail = new PHPMailer(); // Instantiate the PHPMailer Class
  61. $mail->IsSMTP(); // enable SMTP
  62. $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
  63. $mail->SMTPAuth = true; // SMTP authentication enabled
  64. $mail->SMTPSecure = 'ssl'; // secure transfer enabled + REQUIRED for Gmail (either SSL or TLS)
  65. $mail->Host = "smtp.gmail.com"; //Gmail SMTP Server to relay thru
  66. $mail->Port = 465; // Port 465 as we're using SSL... or use Port 587 for TLS
  67. $mail->IsHTML(true); // We're sending a HTML formatted message
  68. $mail->Username = "....@gmail.com"; // Gmail account for authentication
  69. $mail->Password = "*********"; // Gmail password for authentication
  70. $mail->SetFrom("....@gmail.com"); // The email is being sent from this address
  71. $mail->Subject = "Website Contact Form Enquiry"; // The subject line of the email
  72. $mail->Body = ($message); // The actual email message to be sent
  73. $mail->AddAddress("....@gmail.com"); // The email is being sent to this address
  74.  
  75. if(!$mail->send()) {
  76. echo json_encode(['success' => false, 'message' => 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo]);
  77. exit;
  78. }
  79.  
  80. error_log("Data: ".$data['success']." Message: ".$data['message']);
  81. echo json_encode(['success' => true, 'message' => 'Thanks! We have received your message.']);
  82.  
  83. } else {
  84. echo json_encode(['success' => false, 'message' => 'Please fill out the form completely.']);
  85. }
  86. ?>
  87.  
  88. //var request = $http({
  89. //It returns a promise
  90. var promise = $http({
  91. method : 'POST',
  92. url : 'php/contact.php',
  93. data : $.param($scope.formData), //param method from jQuery
  94. headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
  95. });
  96. //Use .then method to receive response
  97. promise.then(function (response) {
  98. var request = response.data;
  99. if (request.success) {
  100. console.log(request);
  101. $scope.submitButtonDisabled = false;
  102. $scope.result='bg-success';
  103. $scope.resultMessage = request.message;
  104. }
  105. });
  106.  
  107. app.controller('ContactController', function ($scope, $http) {
  108. $scope.result = 'hidden'
  109. $scope.resultMessage;
  110. $scope.formData; //formData is an object holding the name, email, subject, and message
  111. $scope.submitButtonDisabled = false;
  112. $scope.submitted = false;
  113. $scope.submit = function(contactform) {
  114. $scope.submitted = true;
  115. $scope.submitButtonDisabled = true;
  116. var promise = $http({
  117. method : 'POST',
  118. url : 'php/contact.php',
  119. data : {
  120. firstname: $scope.formData.inputFirstName,
  121. lastname: $scope.formData.inputLastName,
  122. emailid: $scope.formData.inputEmail,
  123. phoneno: $scope.formData.inputPhone,
  124. message: $scope.formData.inputMessage
  125. },
  126. headers : {'Content-Type': 'application/json'}
  127. })
  128. promise.then(function (response) {
  129. var request = JSON.stringify(response.data); //convert JSON data to string for manipulation
  130. var startpos = request.indexOf("{"); //locate '{' as its the start of the data we want
  131. var endpos = request.lastIndexOf("}"); //locate '}' as its the end of the data we want
  132. var res = request.slice(startpos, endpos); //Extract the actual data now we know where it is.
  133. var newresponse = res.split("\"); //Split the data into new array
  134. var answer = request.search("true"); //search the string to see if it contains the word "true" meaning an email was sent.
  135.  
  136. if (answer >= 0) {
  137. $scope.submitButtonDisabled = false;
  138. $scope.result='bg-success';
  139. $scope.resultMessage = newresponse[5].replace('"', " ");
  140. } else {
  141. $scope.submitButtonDisabled = true;
  142. $scope.resultMessage = newresponse[5].replace('"', " ");
  143. $scope.result='bg-danger';
  144. }
  145. });
  146. }
  147. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement