angularjs php中的简单形式

时间:2014-01-28 09:09:27

标签: php forms angularjs

我的表单代码是

      <form name="myForm" ng-controller="Ctrl" action="form.php" method="POST">
      Name: <input name="input" ng-model="userName" name="name" required placeholder="Enter Name"><br>
      Email: <input name="email" ng-model="userEmail" name="email" required placeholder="Enter Email"><br>
      <input type="submit" name="submit"><br>
       <tt>userType = {{userName}}</tt><br>
       <tt>userEmail = {{userEmail}}</tt><br>
      </form>

我的脚本是

<script >
    function Ctrl($scope) {
  $scope.userName = '';
  $scope.userEmail = '';
}
</script>

form.php代码是

if(isset($_POST['submit']))
    {
        $name=$_POST['name'];
        $email = $_POST['email'];
        echo $name."<br>";
        echo $email."<br>";
    }

如何将表单值传递给php,任何想法

2 个答案:

答案 0 :(得分:1)

我更愿意将模型保留在对象中:

这是一个吸虫:http://plnkr.co/edit/fC1GikCS0v1tDVTG37vZ?p=preview

<强>控制器

function Ctrl($scope, $http) {

  $scope.user = {
    name : '',
    email: ''
  };

  $scope.submit = function(user){    

     $http.post('/form.php', user);

  }
}

<强> HTML

<form name="myForm" ng-controller="Ctrl" ng-submit="submit(user)">

  Name: <input type="text" ng-model="user.name"><br>
  Email: <input type="email" ng-model="user.email"><br>

  <input type="submit" name="submit"><br>

</form>

答案 1 :(得分:0)

在上面的代码中你使用名称标签两次.. 这是在PHP代码中它没有拿起数据。