angular.js:发送一些数据的PHP脚本 [英] angular.js: Send some data to php-script
问题描述
这是我的一个angular.js SPA的模板:
This is my template of an angular.js SPA:
<div data-id="{{id}}" ng-repeat="id in array.id">
<input type="text" name="value1" value="{{value1}}">
<input type="text" name="value2" value="{{value2}}">
</div>
此模板的数据是通过在控制器HTTP POST请求加载。
The data for this template is loaded via http post request in a controller.
现在我想把这个数据发送回PHP脚本,将其保存在数据库中。我想这样做,在一个指令,但我不知道如何将数据发送动态 - 因为有多个行和一些模板不同,一点点
Now I want to send this data back to the php-script to save it in the DB. I would like to do that in a directive, but I don't know how to send the data dynamically - as there are multiple rows and some templates differ a little bit.
推荐答案
首先...使用NG-模式,它会让你的生活变得更轻松。
First off... USE ng-model, it will make your life so much easier
<div data-id="{{id}}" ng-repeat="id in array.id">
<input type="text" name="value_{{$index}}" ng-model="id.value">
</div>
二,你的控制器中,你将需要使用$ http.post将数据发送到PHP端点/脚本。
Second, within your controller, you are going to need to use $http.post to send the data to your php endpoint/script.
app.controller("SomeController", function($scope, $http){
$scope.sendFunction = function (array) {
var data = {
array: array // The array of data you are passing in
}
$http.post('yourPhpEndpoint.php', data).success(function(data){
//This means that you have succeeded
}).error(function(){
//This means that it failed
})
}
})
三,我会假设你正在使用一个标准化的数据库,并想接一个进入这些数组到一个表中的一个......在你的PHP文件。
Third, I'm going to assume that you are using a normalized database and would like to enter these arrays into a table one by one... In Your PHP File.
$data = json_decode(file_get_contents('php://input'));
$submittedInfo = $data->array;
foreach($submittedInfo as $arrayItem){
// Do an insert into the database
}
这篇关于angular.js:发送一些数据的PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!