AngularJS $http POST to PHP not working -
i attempting post data server using angular $http , having no success. have 2 issues:
1) form nothing when button clicked. no post request sent. when add action="include/index.php"
form element, post request sent, page redirects php script , displays response. have tried setting action=""
.
i have attempted removing ng-submit
form , added ng-click
on submit button no success. have confirmed data correctly binding controller.
2) php not able read post data. when using action="include/index.php"
on form , entering valid data, php returns validation errors. seems php not able access data properly.
i've added json_decode(file_get_contents('php://input'));
php script. have tried changing request content type 'application/x-www-form-urlencoded'
. have using jquery's .param
on form data in controller.
angular http post php , undefined
i realize question has been asked before, none of solutions have worked me. assistance appreciated.
index.php --the form
<body ng-app="contactapp"> <div class="col-md-12" ng-controller="contactcontroller"> <form id="contactform" ng-submit="submitform()" role="form" method="post"> <legend>send message</legend> <div id="result" ng-show="result">{{ result }}</div> <div class="form-group" ng-class="{ 'has-error' : errorname }"> <label for="name">name</label> <input ng-model="formdata.name" type="text" class="form-control" name="name" id="name" placeholder="your name"> <span class="help-block" ng-show="errorname">{{ errorname }}</span> </div> <div class="form-group" ng-class="{ 'has-error' : erroremail }"> <label for="email">email address</label> <input type="email" ng-model="formdata.email" class="form-control" name="email" id="email" placeholder="email"> <span class="help-block" ng-show="erroremail">{{ erroremail }}</span> </div> <div class="form-group" ng-class="{ 'has-error' : errormessage }"> <label for="message">message</label> <textarea class="form-control" ng-model="formdata.message" rows="5" name="message" id="message"></textarea> <span class="help-block" ng-show="errormessage">{{ errormessage }}</span> </div> <div class="form-group" ng-class="{ 'has-error' : errorhuman }"> <label for="human">what 6 + 6 ?</label> <input type="text" ng-model="formdata.human" class="form-control" id="human" name="human" placeholder="your answer"> <span class="help-block" ng-show="errorhuman">{{ errorhuman }}</span> </div> <input type="submit" name="submit" id="submit" value="submit" class="btn btn-default center-block"> </form> </div>
app.js
angular.module('contactapp', []) .controller('contactcontroller', ['$scope', '$http', function($scope, $http) { "use strict"; $scope.formdata = {}; $scope.submitform = function() { $http({ method : 'post', url : 'include/mail.php', data : $scope.formdata, headers : {'content-type': 'application/json'} }) .then(function(response) { if (!response.data.success) { $scope.errorname = response.data.errors.name; $scope.erroremail = response.data.errors.email; $scope.errormessage = response.data.errors.message; $scope.errorhuman = response.data.errors.human; $scope.message = response.data.result; } else { $scope.message = response.data.result; } }); }; }]);
mail.php
<?php include 'config.php'; $errors = array(); $data = array(); $input = json_decode(file_get_contents('php://input')); $name = check_input($input -> name); $email = check_input($input -> email); $message = check_input($input -> message); $human = intval($input -> human); // check if name entered , letters if ($name === '') { $errors['name'] = 'name required.'; } else if (!preg_match("/^[a-za-z ]*$/", $name)) { $errors['name'] = 'only letters , spaces allowed.'; } // check if email entered , valid email address if ($email === '') { $errors['email'] = 'please enter email address.'; } else if (!filter_var($email, filter_validate_email)) { $errors['email'] = 'please enter valid email address'; } //check if message entered if ($message === '') { $errors['message'] = 'please enter message.'; } //check if anti-bot test entered , correct if ($human === '') { $errors['human'] = "please enter answer."; } else if ($human !== 12) { $errors['human'] = 'wrong answer. please try again.'; } if (!empty($errors)) { $data['success'] = false; $data['errors'] = $errors; $data['result'] = 'please correct errors.'; } else{ $to = $toemail; $subject = 'message mydomain.com'; $body = "from: $name\n" . "e-mail: $email\n" . "phone: $phone\n" . "message: $message\n" . "ip address: $ip"; $headers = "from:$email\r\nreply-to:$email"; if (!mail($to, $subject, $body, $headers)) { $data['success'] = false; $data['result'] = 'message not sent. please try again later'; } else { $data['success'] = true; $data['result'] = 'thank you. message has been sent.'; } } header('content-type: application/json'); echo json_encode($data); //sanitize data inputs function check_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?>
edit: have updated code based on comments, still not resolve issues.
Comments
Post a Comment