2014-02-12 2 views
2

데이터를 db로 편집하고 순간 xeditable을 사용하여 뷰를 만들지 만 데이터를 process.php로 전달할 수 없습니다 (db를 업데이트 할 수 없음). 내가 사용하여 데이터를 전달할 수있는 방법 및 데이터)

를 다시 검색

코드 스 니펫 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

.....

,451,515,
<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

$ scope.saveUser = function()에 "return $ http.post ('/ saveUser', $ scope.user)"줄이 먼저 있기 때문에 process.php가 호출되지 않아 즉시 실행이 종료됩니다 saveUser 함수 – Giuseppe

답변

0

당신이나 다른 사람에게 도움이 될 것인지 모르겠어요. Afaik, http.post와 angle을 사용하여 데이터베이스를 업데이트 할 수 없습니다 (필자의 경우 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; 
    ... 
?> 

을 데이터베이스를 업데이트 할 수 있습니다.

업데이트 : $ scope.saveUser = function()에 "return $ http.post ('/ saveUser', $ scope.user)"줄이 먼저 있기 때문에 process.php가 호출되지 않고 즉시 종료됩니다 saveUser 함수의 실행. return statement in Javascript 참조 :

return 문을 함수에서 호출하면이 함수의 실행이 중지됩니다. 지정된 경우, 주어진 값이 함수 호출자에게 리턴됩니다. 표현식이 생략되면 undefined가 대신 반환됩니다.