2014-02-12 30 views
2

我需要编辑数据到数据库并使其成为立即使用角度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.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> 

HTML

.....

<div ng-app="app" ng-controller="Ctrl"> 

         <form editable-form name="editableForm" > 
         <ul> 
        <li class="err" ng-repeat="error in errors"> {{ error}} </li> 
       </ul> 
       <ul> 
        <li class="info" ng-repeat="msg in msgs"> {{ msg}} </li> 
       </ul> 
          <div class="pull-right"> 
          <!-- button to show form --> 
          <button type="button" class="btn btn-default btn-sm" ng-click="editableForm.$show()" ng-show="!editableForm.$visible"> Edit </button> 
          <!-- buttons to submit/cancel form --> 
          <span ng-show="editableForm.$visible"> 
          <button type="submit" class="btn btn-primary btn-sm" ng-disabled="editableForm.$waiting" onaftersave="saveUser()"> Save </button> 
          <button type="button" class="btn btn-default btn-sm" ng-disabled="editableForm.$waiting" ng-click="editableForm.$cancel()"> Cancel </button> 
          </span> </div> 
          <div> 
          <!-- editable username (text with validation) --> 
          <span class="title">User name: </span> <span editable-text="user.name" e-id="myid" e-name="name" e-required>{{ user.name || 'empty' }}</span> 
          </div> 
          <div> 
          <!-- editable status (select-local) --> 
          <span class="title">Status: </span> <span editable-select="user.status" e-name="status" e-ng-options="s.value as s.text for s in statuses"> {{ (statuses | filter:{value: user.status})[0].text || 'Select' }} </span> </div> 
          <div> 
          <!-- editable group (select-remote) --> 
          <span class="title">Group: </span> <span editable-select="user.group" e-name="group" e-ng-options="g.value as g.text for g in groups"> {{ (groups | filter:{value: user.group})[0].text || 'Select' }} </span> </div> 
         </form> 
         </div> 

....

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); 
} 
?> 
+0

您的process.php不会被调用,因为您在$ scope.saveUser = function()中首先返回$ http.post('/ saveUser',$ scope.user)“行,该行立即结束执行saveUser函数 – Giuseppe

回答

0

我不知道这是否会是你或别人有用。 012faAfaik,你不能使用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; 
    ... 
?> 

与AT $名称,$地位,你会$组值能够更新你的数据库。

UPDATE:您process.php不叫,因为你有一个 “返回$ http.post( '/ saveUser',$ scope.user)” 行头在$ scope.saveUser =()的函数,它立即结束执行saveUser函数。请参阅return statement in Javascript

当在函数中调用return语句时,停止执行此函数。如果指定,则将给定值返回给函数调用方。如果省略表达式,则返回undefined。