我正在尝试通过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/