javascript - Email sending with AngularJS and PHP -
i have created application in angularjs , has email contact form , sends emails using php file in server.
here's controller.js part in angularjs:
$scope.feedbacksubmit= function (){ var name1 = document.getelementbyid("name").value; var email1 = document.getelementbyid("email").value; var message1 = document.getelementbyid("message").value; $http({ url: "http://boost.meximas.com/mobile/email.php", method: "post", data: { name: name1, email: email1, message:message1 } }).success(function(data, status, headers, config) { // callback called asynchronously // when response available if(status == 200) { var return_data = data; if(return_data != 0){ $scope.hide(); //$scope.closefeedback(); }else{ $scope.showemailerror(); } } }). error(function(data, status, headers, config) { // called asynchronously if error occurs // or server returns response error status. console.log(status); $scope.showalertnetwork(); $scope.hide(); }); };
here's php code:
<?php $array = json_decode(file_get_contents('php://input'), true); $name = $array['name']; $email = $array['email']; $message = $array['message']; if (($name=="")||($email=="")||($message=="")) { printf("0"); } else{ $from="from: $name<$email>\r\nreturn-path: $email"; $subject="message sent using contact form"; mail("mygmail@gmail.com", $subject, $message, $from); } ?>
the problem arises when fill in contact form , hit send button. i'm getting $scope.showemailerror();
. email without problem.
and if try hit button without filling form still getting same $scope.showemailerror();
message.
why don't use model values input? looks try use angularjs still think other frameworks in mind
in following example i'm sending model php script, if name not filled php returns 404 error , it's handled in angularjs $http
via .error
handler. don't have add logic success deal it
http://plnkr.co/edit/sw9rrxb3kdewxszjdwx3?p=preview
html
<input type="text" ng-model="formdata.name" placeholder="name"> <input type="text" ng-model="formdata.email" placeholder="email"> <input type="text" ng-model="formdata.message" placeholder="message">
javascript
$scope.formdata = { 'name': '', 'email': '', 'message': '' }; $scope.postdata = function () { $http.post('http://edeen.pl/form.php', $scope.formdata) .success( function(data){ $scope.response = data.replace(/\ /g, ' ').replace(/\n/g, '<br/>') //format response }) .error( function(data){ $scope.response = data }) }
php
$input = json_decode(file_get_contents('php://input')); if($input->name === ''){ header("http/1.0 404 not found"); echo "something went terribly wrong, missing name maybe?"; return; } header('content-type: application/json'); var_dump($input);
Comments
Post a Comment