· 9 years ago · Jan 09, 2017, 03:34 AM
1app.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$json = file_get_contents('php://input');
108$obj = json_decode($json);
109
110var promise = $http({
111 method : 'POST',
112 url : 'php/contact.php',
113 //data : $.param($scope.formData), //param method from jQuery
114 data: $scope.data;
115 //headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
116 //Defaults to:
117 //headers: {'Content-Type': 'application/json'}
118 });
119 //Use .then method to receive response
120 promise.then(function (response) {
121 var request = response.data;
122 if (request.success) {
123 console.log(request);
124 $scope.submitButtonDisabled = false;
125 $scope.result='bg-success';
126 $scope.resultMessage = request.message;
127 }
128 });
129
130app.controller('ContactController', function ($scope, $http) {
131 $scope.result = 'hidden'
132 $scope.resultMessage;
133 $scope.formData; //formData is an object holding the name, email, subject, and message
134 $scope.submitButtonDisabled = false;
135 $scope.submitted = false;
136 $scope.submit = function(contactform) {
137 $scope.submitted = true;
138 $scope.submitButtonDisabled = true;
139 var promise = $http({
140 method : 'POST',
141 url : 'php/contact.php',
142 data : {
143 firstname: $scope.formData.inputFirstName,
144 lastname: $scope.formData.inputLastName,
145 emailid: $scope.formData.inputEmail,
146 phoneno: $scope.formData.inputPhone,
147 message: $scope.formData.inputMessage
148 },
149 headers : {'Content-Type': 'application/json'}
150 })
151 promise.then(function (response) {
152 var request = JSON.stringify(response.data); //convert JSON data to string for manipulation
153 var startpos = request.indexOf("{"); //locate '{' as its the start of the data we want
154 var endpos = request.lastIndexOf("}"); //locate '}' as its the end of the data we want
155 var res = request.slice(startpos, endpos); //Extract the actual data now we know where it is.
156 var newresponse = res.split("\"); //Split the data into new array
157 var answer = request.search("true"); //search the string to see if it contains the word "true" meaning an email was sent.
158
159 if (answer >= 0) {
160 $scope.submitButtonDisabled = false;
161 $scope.result='bg-success';
162 $scope.resultMessage = newresponse[5].replace('"', " ");
163 } else {
164 $scope.submitButtonDisabled = true;
165 $scope.resultMessage = newresponse[5].replace('"', " ");
166 $scope.result='bg-danger';
167 }
168 });
169 }
170 });