使用AngularJS将表单发布到PHP

时间:2015-06-07 15:11:28

标签: javascript php angularjs ionic-framework

我一直在使用AngularJS构建Ionic Mobile应用程序。我试图发送电子邮件表格。 PHP部分工作正常。我用参数化的URL测试它,它工作。但是应用程序无法正常工作。

这是我的HTML部分

    <form>
      <div class="list list-inset">
      <label class="item item-input">
        <input type="text" placeholder="Name" ng-model="name">
      </label>
      <label class="item item-input">
        <input type="text" placeholder="Email" ng-model="email">
      </label>
      <label class="item item-input">
        <textarea row="10" cols="50" placeholder="Message" ng-model="message"></textarea>
      </label>
       <label class="item">
              <button class="button button-block button-positive" ng-click="feedbacksubmit();">Submit</button>
        </label>
       </div>

</form>

和我的ControllerJS部分

$scope.feedbacksubmit= function (){  
  $scope.buttonclick() // to Check button has click (yes button works)
  $http.post("http://boost.meximas.com/mobile/email.php?name="+name+"&email="+email+"&message="+message).success(function(data){
  //$scope.tasks = data;
  $scope.donemessage();
});
};

这是我的PHP代码

<?php 

    $name=$_GET['name']; 
    $email=$_GET['email']; 
    $message=$_GET['message']; 

    if (($name=="")||($email=="")||($message=="")) 
        { 
        echo "All fields are required, please fill <a href=\"\">the form</a> again."; 
        } 
    else{         
        $from="From: $name<$email>\r\nReturn-path: $email"; 
        $subject="Message sent using your contact form"; 
        mail("testing@gmail.com", $subject, $message, $from); 
        echo "Email sent!"; 
        } 

?> 

2 个答案:

答案 0 :(得分:1)

尝试将数据作为JSON对象发布,并将其作为$http.post()的第二个参数传递。

var data = {
    name: 'foo bar',
    email: 'foo@bar.com'
};
$http.post("http://boost.meximas.com/mobile/email.php", data)
   .success(function(data){
       //$scope.tasks = data;
       $scope.donemessage();
});

答案 1 :(得分:0)

示例代码和JsFiddle:http://jsfiddle.net/YGQT9/

app.controller('FormCtrl', function ($scope, $http) {    
    $scope.data = {
        firstname: "default",
        emailaddress: "default",
        gender: "default",
        member: false,
        file_profile: "default",
        file_avatar: "default"
    };
    $scope.submitForm = function() {
        console.log("posting data....");
        $http.post('http://posttestserver.com/post.php?dir=jsfiddle', JSON.stringify(data)).success(function(){/*success callback*/});
    };
});