2017-11-29 9 views
0

私はPHPでoopsを使用していますが、特定のメソッドにangularjを使ってデータを投稿したいのですが、多くのリンクとサイトを検索しました。問題ありがとう。angjs oopsを使用しているときのPHPのポストへの投稿

これは私のjsスクリプト

 <script type="text/javascript"> 
     function FrmController($scope, $http) { 
      $scope.errors = []; 
      $scope.msgs = []; 

      $scope.SignUp = function() { 

       $scope.errors.splice(0, $scope.errors.length); // remove all error messages 
       $scope.msgs.splice(0, $scope.msgs.length); 

       $http.post('post_es.php', {'uname': 'testName', 'pswd': 'testPass', 'email': 'testEmail'} 
       ).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); 
       }); 
      } 
     } 
    </script> 

であり、これはPHP

<?php 
    class sampleTest { 
     public function firstMethod(){ 
     $data = json_decode(file_get_contents("php://input")); 
     $usrname = mysql_real_escape_string($data->uname); 
     $upswd = mysql_real_escape_string($data->pswd); 
     $uemail = mysql_real_escape_string($data->email); 
     } 
    } 

である、それは特定のメソッドを取得/ポストを使用して投稿することも可能であるあなたが最初のアプリを宣言しなければなりません

答えて

0

とコントローラを宣言してください:

var app=angular.module("app",[]); 
app.controller("myCtrl",function FrmController($scope, $http) { 
      $scope.errors = []; 
      $scope.msgs = []; 
      $scope.SignUp = function() { 
       $scope.errors.splice(0, $scope.errors.length); // remove all error messages 
       $scope.msgs.splice(0, $scope.msgs.length); 
       $http.post('post_es.php', {'uname': 'testName', 'pswd': 'testPass', 'email': 'testEmail'} 
       ).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); 
       }); 
      } 
     }); 

ng-appとng-controllerのディレクティブでアプリとコントローラをリンクする必要があります。 https://docs.angularjs.org/api/ng/directive/ngController

関連する問題