2017-01-23 66 views
0

我有一個使用CodeIgniter製作的項目,我們希望使用PHP作爲服務器,而AngularJS作爲客戶端。我想太多插入名稱和用戶到我們的數據庫 的ID這是AngularJS代碼:如何使用CodeIgniter控制器將數據從AngularJS表單插入到數據庫中?

<!doctype html> 
 
<html> 
 
<head> 
 
    <title>Angular Forms</title> 
 

 
    <!-- LOAD BOOTSTRAP CSS --> 
 
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css"> 
 

 
    <!-- LOAD JQUERY --> 
 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> 
 
    <!-- LOAD ANGULAR --> 
 
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script> 
 

 
    
 
    <script> 
 
    // define angular module/app 
 
    var formApp = angular.module('formApp', []); 
 
    // create angular controller and pass in $scope and $http 
 
    function formController($scope, $http) { 
 
     // create a blank object to hold our form information 
 
     // $scope will allow this to pass between controller and view 
 
     $scope.formData = {}; 
 
     // process the form 
 
     $scope.processForm = function() { 
 
     $http({ 
 
       method : 'POST', 
 
       url  : 'http://localhost/alpha2/index.php/user/insert', 
 
       data : $.param($scope.formData), // pass in data as strings 
 
       headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload) 
 
      }) 
 
       .success(function(data) { 
 
        console.log(data); 
 
        if (!data.success) { 
 
        // if not successful, bind errors to error variables 
 
         $scope.erroridAlumno = data.errors.idAlumno; 
 
         $scope.errorNombre = data.errors.nombre; 
 
        } else { 
 
        // if successful, bind success message to message 
 
         $scope.message = data.message; 
 
         $scope.erroridAlumno = ''; 
 
         $scope.errorNombre = ''; 
 
        } 
 
       }); 
 
     }; 
 
    } 
 
    </script> 
 
     <style> 
 
      .ng-valid.ng-dirty{ 
 
       border-color: green; 
 
      } 
 
      .ng-invalid.ng-dirty{ 
 
       border-color: red; 
 
      } 
 
     </style> 
 
</head> 
 
<!-- apply the module and controller to our body so angular is applied to that --> 
 
<body ng-app="formApp" ng-controller="formController"> 
 
<div class="container"> 
 
<div class="col-md-6 col-md-offset-3"> 
 

 
    <!-- PAGE TITLE --> 
 
    <div class="page-header"> 
 
    <h1><span class="glyphicon glyphicon-tower"></span> Insertar Nuevo Alumno</h1> 
 
    </div> 
 

 
    <!-- SHOW ERROR/SUCCESS MESSAGES --> 
 
    <div id="messages" class="well" ng-show="message">{{ message }}</div> 
 

 
    <!-- FORM --> 
 
    <form ng-submit="processForm()"> 
 
    <!-- ID --> 
 
    <div id="idAlumno-group" class="form-group" ng-class="{ 'has-error' : erroridAlumno }"> 
 
     <label>ID</label> 
 
     <input type="text" name="idAlumno" class="form-control" placeholder="Bruce Wayne" ng-model="formData.idAlumno"> 
 
         <span class="help-block" ng-show="erroridAlummno">{{ erroridAlumno }}</span> 
 
    </div> 
 

 
    <!-- NAME --> 
 
    <div id="nombre-group" class="form-group" ng-class="{ 'has-error' : errorNombre }"> 
 
     <label>Nombre</label> 
 
     <input type="text" name="nombre" class="form-control" placeholder="Caped Crusader" ng-model="formData.nombre"> 
 
     <span class="help-block" ng-show="errorNombre">{{ errorNombre }}</span> 
 
    </div> 
 

 
    
 

 
    <!-- SUBMIT BUTTON --> 
 
    <button type="submit" class="btn btn-success btn-lg btn-block"> 
 
     <span class="glyphicon glyphicon-flash"></span> Submit! 
 
    </button> 
 
    </form> 
 

 
    <!-- SHOW DATA FROM INPUTS AS THEY ARE BEING TYPED --> 
 
    <pre> 
 
    {{ formData }} 
 
    </pre> 
 

 
</div> 
 
</div> 
 
</body> 
 
</html>

而在我的PHP項目,我有一個控制器(和模型)名爲「用戶」。 該控制器有三種方法:索引,用戶和插入。 插入是我試圖使它工作的方法,我用來將新數據插入到數據庫中的方法。

我認爲這可能是與routes.php(在config文件夾)的問題,我不知道該放什麼。

用戶/插入將數據插入數據庫的正確代碼是什麼? route.php的正確代碼是什麼?

用戶實體的模型名爲「user_model」。 這是模型的代碼:

<?php 
 
class User_model extends CI_Model { 
 

 

 

 
     public function __construct() 
 
     { 
 
       $this->load->database(); 
 
     } 
 

 
\t \t public function get_users($id = FALSE) 
 
\t \t { 
 
\t \t   if ($id === FALSE) 
 
\t \t   { 
 
\t \t     $query = $this->db->get('alumno'); 
 
\t \t     return $query->result_array(); 
 
\t \t   } 
 

 
\t \t   $query = $this->db->get_where('alumno', array('idAlumno' => $id)); 
 
\t \t   return $query->row_array(); 
 
\t \t } 
 

 
\t \t public function insert_user($data){ 
 
\t \t \t 
 
     $query = $this->db->insert('alumno',$data); 
 
     return $query; 
 
    } 
 

 
\t \t 
 

 

 
} 
 

 

 
?>

( 「alumno」 是表的名字在我的數據庫)

謝謝您的幫助。

+0

首先,你必須確保你的服務器端控制器接受POST請求。因此,相應地爲routes.php和控制器端編寫代碼。獲取從角度使用請求對象或$ _POST發佈的數據...我認爲這就是所有你需要編寫的更多 – webDev

+0

在codeigniter上獲取發佈數據的方法是「$ this-> input-> post('nameOfTheVariable')」。但我不知道如何在routes.php和控制器中編寫正確的代碼。 –

+0

這是你正在尋找http://stackoverflow.com/questions/29159612/codeigniter-how-to-set-a-controller-function-to-be-a-post-route或直接REST端點與codeigniter https :// github上。com/chriskacerguis/codeigniter-restserver – webDev

回答

0

如果您已經使用笨,restserver那麼你需要有方法的名稱相應支持HTTP請求不同的方法。

例如: 如果你正在做一個HTTP GET調用以/books,例如,它會召喚Books#index_get()方法。

同樣,你的情況你的方法名稱必須是insert_post()在您的控制器從角AJAX POST支持你的POST請求。

所以,在你的用戶控制器必須有一個方法insert_post()支持URL ...../user/insert

請參見處理HTTP請求Handling Request

相關問題