I have built a contact form with AngularJS and using PHP. Form is working perfectly and I able to receive emails. However, after the form is submitted I want to disable the submit button and display success message and vice - versa.
My code are as follows:
var app = angular.module('myApp', ['jcs-autoValidate']);
app.run(['defaultErrorMessageResolver',
function(defaultErrorMessageResolver) {
defaultErrorMessageResolver.getErrorMessages().then(function (errorMessages) {
errorMessages['validPhone'] = 'Please enter valid phone number';
});
}]);
app.controller('FormController', function($scope, $http){
$scope.formData = {};
$scope.result = 'hidden'
$scope.submitButtonDisabled = false;
$scope.submitted = false;
$scope.submit = function(help) {
$http({
method : 'POST',
url : 'process.php',
data : $.param($scope.formData),
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
}) .success(function(data) {
console.log(data);
if (data.success) {
$scope.submitButtonDisabled = true;
$scope.resultMessage = data.message;
$scope.result = 'bg-sucess';
} else {
$scope.submitButtonDisabled = false;
$scope.resultMessage = data.message;
$scope.result = 'bg-danger';
}
});
}
});
PHP
<?php
require_once 'phpmailer/PHPMailerAutoload.php';
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['phone']) && isset($_POST['message'])) {
if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['phone']) || empty($_POST['message'])) {
$data = array('success' => false, 'message' => 'Please the form completely.');
echo json_encode($data);
exit;
}
$mail = new PHPMailer();
$mail->From = $_POST['email'];
$mail->FromName = $_POST['name'];
$mail->AddAddress('xxxx@gmail.com');
$mail->Subject = $_POST['phone'];
$mail->Body = "Name: " . $_POST['name'] . "\r\n\r\nMessage: " . stripslashes($_POST['message']);
if (isset($_POST['ref'])) {
$mail->Body .= "\r\n\r\nRef: " . $_POST['ref'];
}
if(!$mail->send()) {
$data = array('success' => false, 'message' => 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo);
echo json_encode($data);
exit;
}
$data = array('success' => true, 'message' => 'Thank you for contacting us! We will get back to you soon.');
echo json_encode($data);
} else {
$data = array('success' => false, 'message' => 'Please the form completely.');
echo json_encode($data);
}
CONSOLE:
{"success":true,"message":"Thank you for contacting us! We will get back to you soon."}
You can also access the demo link for more information http://newangularform.comlu.com/
Thanks for any help in advance