Php Angular.js xeditable$http.post数据处理
我需要编辑数据库中的数据,并使用angular xeditables使其成为即时视图,但我无法将数据传递到process.php(以更新数据库并检索数据) 如何使用$http.post传递数据 代码片段Php Angular.js xeditable$http.post数据处理,php,ajax,json,angularjs,x-editable,Php,Ajax,Json,Angularjs,X Editable,我需要编辑数据库中的数据,并使用angular xeditables使其成为即时视图,但我无法将数据传递到process.php(以更新数据库并检索数据) 如何使用$http.post传递数据 代码片段 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script> <script src="http://code.angularjs.org/1
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<script src="http://code.angularjs.org/1.0.8/angular-mocks.js"></script>
<link href="angular-xeditable-0.1.8/css/xeditable.css" rel="stylesheet">
<script src="angular-xeditable-0.1.8/js/xeditable.js"></script>
<script>
var app = angular.module("app", ["xeditable", "ngMockE2E"]);
app.run(function(editableOptions) {
editableOptions.theme = 'bs3';
});
app.controller('Ctrl', ['$scope','$filter','$http', function($scope, $filter,$http) {
$scope.user = {
name: 'awesome user',
status: 2,
group: 4
};
$scope.statuses = [
{value: 1, text: 'status1'},
{value: 2, text: 'status2'},
{value: 3, text: 'status3'},
{value: 4, text: 'status4'}
];
$scope.groups = [
{value: 1, text: 'user'},
{value: 2, text: 'customer'},
{value: 3, text: 'vip'},
{value: 4, text: 'admin'}
];
$scope.errors = [];
$scope.msgs = [];
$scope.saveUser = function()
{ // $scope.user already updated!
return $http.post('/saveUser', $scope.user).error(function(err) {});
$scope.errors.splice(0, $scope.errors.length); // remove all error messages
$scope.msgs.splice(0, $scope.msgs.length);
$http.post('include/process.php', {'name': $scope.name, 'status': $scope.status, 'group': $scope.group}
).success(function(data, status, headers, config) {
if (data.msg != '')
{
$scope.msgs.push(data.msg);
}
else
{
$scope.errors.push(data.error);
}
}).error(function(data, status) { // called asynchronously if an error occurs
// or server returns response with an error status.
$scope.errors.push(status);
});
};
}]);
app.run(function($httpBackend) {
$httpBackend.whenPOST(/\/saveUser/).respond(function(method, url, data) {
data = angular.fromJson(data);
});
});
</script>
var app=angular.module(“app”、[“xeditable”、“ngMockE2E”);
应用程序运行(功能(可编辑选项){
editableOptions.theme='bs3';
});
app.controller('Ctrl'、['$scope'、'$filter'、'$http',函数($scope、$filter、$http){
$scope.user={
名称:“超级用户”,
现状:2,
组别:4
};
$scope.status=[
{值:1,文本:'status1'},
{值:2,文本:'status2'},
{值:3,文本:'status3'},
{值:4,文本:'status4'}
];
$scope.groups=[
{value:1,文本:'user'},
{value:2,文本:'customer'},
{值:3,文本:'vip'},
{值:4,文本:'admin'}
];
$scope.errors=[];
$scope.msgs=[];
$scope.saveUser=function()
{/$scope.user已更新!
返回$http.post('/saveUser',$scope.user).error(函数(err){});
$scope.errors.splice(0,$scope.errors.length);//删除所有错误消息
$scope.msgs.splice(0,$scope.msgs.length);
$http.post('include/process.php',{'name':$scope.name,'status':$scope.status,'group':$scope.group}
).success(函数(数据、状态、标题、配置){
如果(data.msg!='')
{
$scope.msgs.push(data.msg);
}
其他的
{
$scope.errors.push(data.error);
}
}).error(如果发生错误,则异步调用函数(数据、状态){//
//或服务器返回带有错误状态的响应。
$scope.errors.push(状态);
});
};
}]);
app.run(函数($httpBackend){
$httpBackend.whenPOST(/\/saveUser/).respond(函数(方法、url、数据){
data=angular.fromJson(数据);
});
});
HTML
- {{error}
- {{{msg}
编辑
拯救
取消
用户名:{{User.name | |'empty'}
状态:{(状态|筛选器:{value:user.Status})[0]。文本| |'选择'}
组:{(组|筛选器:{value:user.Group})[0]。文本| |'选择'}
PROCESS.PHP
<?php
$data = json_decode(file_get_contents("php://input"));
$name = mysql_real_escape_string($data->name);
$status = mysql_real_escape_string($data->status);
$group = mysql_real_escape_string($data->group);
if ($group == 'vip') {
if ($qry_res) {
$arr = array('msg' => "User Created Successfully!!!", 'error' => '');
$jsn = json_encode($arr);
print_r($jsn);
} else {
$arr = array('msg' => "", 'error' => 'Error In inserting record');
$jsn = json_encode($arr);
print_r($jsn);
}
} else {
$arr = array('msg' => "", 'error' => 'User Already exists with same email');
$jsn = json_encode($arr);
print_r($jsn);
}
?>
我不知道它是否对你或其他人有用。 好了,您不能将http.post与angular一起使用来更新数据库(至少在我的例子中,使用php和mysql)。您需要使用$http(),如下所示:
var request = $http({
method: "post",
url: "include/process.php",
data: {
name: $scope.name,
status: $scope.status,
group: $scope.group
},
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});
然后,在PHP中,按如下方式检索:
<?php
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
@$name = $request->name;
@$status = $request->status;
@$group = $request->group;
...
?>
使用$name、$status和$group的值,您将能够更新数据库
更新:未调用process.php,因为在$scope.saveUser=function()中首先有一行“return$http.post('/saveUser',$scope.user)”,这将立即结束saveUser函数的执行。见: 在函数中调用return语句时,此函数的执行将停止。如果指定,将向函数调用方返回给定值。如果省略表达式,则返回undefined
不会调用process.php,因为在$scope.saveUser=function()中首先有一行“return$http.post('/saveUser',$scope.user)”,这将立即结束saveUser函数的执行
<?php
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
@$name = $request->name;
@$status = $request->status;
@$group = $request->group;
...
?>