javascript - 将数据从 Angular 形式传递到 PHP

标签 javascript php angularjs

我正在尝试通过ajax将Angular中的表单数据保存到store.php文件中。但是,似乎数据未按文件正确获取。有人可以指出错误吗?

<form id="form1" post="content/store.php">

    <div class="form-group">
        <label for="examplename">Name</label>
        <input type="text" class="form-control" id="examplename" placeholder="Enter your name">
    </div>

    <div class="form-group">
        <label for="exampleemail">Email</label>
        <input type="email" class="form-control" id="exampleemail" placeholder="Enter your email">
    </div>

    <button type="submit" class="btn btn-default" ng-click="submitting()" >Submit</button>          

</form>

Angular 脚本:

var app = angular.module('myApp',[]);
app.controller ('myCtrl',function($scope, $http){
    $scope.submitting = function(){

        var request = $http({
            method: "post",
            url: "localhost/content/store.php",
            data: {
                email: $scope.email,
                pass: $scope.password
            }
        });

        request.success(function (data) {
            alert("Successfully data entered! ");
        });

    }

}); 

STORE.PHP

<?php
  $postdata = file_get_contents("php://input");
  $request = json_decode($postdata);
  @$email = $request->email;
  @$pass = $request->pass;
  echo $email; 
?>

最佳答案

如果你想在你的 Controller 中使用

email: $scope.email

name: $scope.name

或密码,您需要在 View 中进行 ng-model 绑定(bind),如下所示:

<form id="form1" post="content/store.php">

    <div class="form-group">
        <label for="examplename">Name</label>
        <input type="text" class="form-control" id="examplename" placeholder="Enter your name" ng-model="name">
    </div>

    <div class="form-group">
        <label for="exampleemail">Email</label>
        <input type="email" class="form-control" id="exampleemail" placeholder="Enter your email" ng-model="email">
    </div>

    <button type="submit" class="btn btn-default" ng-click="submitting()" >Submit</button>          

</form>

关于javascript - 将数据从 Angular 形式传递到 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31428817/

相关文章:

javascript - 正则表达式在字符的第三个实例之后获取内容

javascript - 如何扩展此 javascript 以在一次单击中更改两个元素的类?

php - 将 MySQL 函数传递给 Eloquent ORM 查询

php - 需要数据库输入帮助

html - ng-model 的 Angularjs 动态名称

javascript - angularjs中ng-repeat值为null时如何显示空字符串

javascript - 如何从日期选择器中获取日期并显示在表格中? Material -UI。响应式JS

php - 尝试创建大数组时的 PHP 问题

javascript - Angular .js : How to get orderBy or filter from an unordered list to work?

mysql - Rails - 公寓 gem - 公共(public)和租户数据库的不同模式