2016-10-18 15 views
0

[編集]投稿データをvarとdelete $ scopeに入れてください。CodeigniterとAngular data post

$scope.deleteData = function(email) { 
if(confirm("Are you sure you want to delete this record?")){ 
var email1= email; $http.post('<?php echo site_url("index.php/user/delete_user");?>', {'email':email1}).error(function(){ console.log("error"); }); 
} 
} 

私が削除ボタンを押しても何も起こりません。 コードを確認し、解決策をご回答ください。ネットワークペイロードは何も渡さない{}。 ビュー/ user_view.php

<!DOCTYPE html > 
 
<html lang="en" ng-app="userApp"> 
 
<head> 
 
<meta charset="utf-8"> 
 
<meta http-equiv="X-UA-Compatible" content="IE=edge"> 
 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script> 
 
<script type="text/javascript" src="<?php echo base_url('assets/jquery/jquery-3.1.1.min.js'); ?>"></script> 
 
<script type="text/javascript" src="<?php echo base_url('assets/bootstrap/js/bootstrap.min.js'); ?>"></script> 
 
<link href="<?php echo base_url('assets/bootstrap/css/bootstrap.min.css'); ?>"> 
 
<script type="text/javascript"> 
 
/*function userController($scope,$http) { 
 
    $scope.users = []; 
 
    $http.get('<?php echo site_url('index.php/user/user_list'); ?>').success(function($data){ $scope.users=$data; }); 
 
}*/ 
 

 
var app = angular.module('userApp', []); 
 
app.controller('userController', function($scope, $http) { 
 

 
\t //$scope.buttonName = "Add"; 
 

 
\t $scope.deleteData = function() { 
 
\t \t /*$http.post("<?php echo base_url(); ?>index.php/User/delete_user/",{'id':$scope.id}) 
 
\t \t .success(function(data,status,headers,config){ 
 
      $scope.message = "Deleted Successfully!"; 
 
\t \t \t 
 
\t \t }); 
 
\t \t */ 
 

 
\t \t $http.post('<?php echo site_url('index.php/user/delete_user');?>', 
 
\t \t \t {'email':$scope.email}).error(function(){ 
 
\t \t \t \t console.log("error"); 
 
\t \t \t }) 
 
\t } 
 
\t $scope.displayUsers = function() { 
 
\t \t  $scope.users = []; 
 
\t \t  $http.get('<?php echo site_url("index.php/user/user_list"); ?>').success(function($data){ $scope.users=$data; }); 
 
\t \t } 
 

 
\t $scope.insertData = function(){ 
 
\t \t \t \t $http.post('<?php echo site_url('index.php/user/insert_user');?>', 
 
\t \t \t \t \t { 
 
\t \t \t \t \t \t 'name':$scope.name, 
 
\t \t \t \t \t \t 'gender':$scope.gender, 
 
\t \t \t \t \t \t 'email':$scope.email, 
 
\t \t \t \t \t \t 'address':$scope.address, 
 
\t \t \t \t \t \t 'phone':$scope.phone 
 
\t \t \t \t \t }).error(function() { 
 
\t \t \t \t console.log("Error"); 
 
\t \t \t \t }); 
 
\t \t \t } 
 

 
\t \t 
 
}); 
 
</script> 
 
<!-- Latest compiled and minified CSS --> 
 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css"> 
 
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> 
 
<!-- WARNING: Respond.js doesn't work if you view the page via file:// --> 
 
<!--[if lt IE 9]> 
 
     <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> 
 
     <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> 
 
    <![endif]--> 
 
</head> 
 
<body ng-controller="userController"> 
 
<!-- ng-app : which tells the Angular framework to parse data from this div --> 
 
<div class="container" ng-init="displayUsers()"> 
 
<button class="btn" data-toggle="modal" data-target="#myModal">Add Users</button> 
 
\t <div class="row"> 
 
\t <div class="col-lg-12 col-md-12"> 
 
\t <input type="search" class="form-control" ng-model="searchUser" placeholder="Live Search"> 
 
\t  <table class="table table-bordered table-condensed table-responsive" ng-controller="userController"> 
 
\t  <thead> 
 
\t   <tr> 
 
\t   <td>Select</td> 
 
\t   <td>Name</td> 
 
\t   <td>Gender</td> 
 
\t   <td>Email</td> 
 
\t   <td>Address</td> 
 
\t   <td>Phone Number</td> 
 
\t   <td>Action</td> 
 
\t   </tr> 
 
\t  </thead> 
 
\t  <tbody> 
 
\t   <tr ng-repeat="user in users | filter:searchUser"> 
 
\t   <td><input type="checkbox"></td> 
 
\t   <td class="hidden">{{user.id}}</td> 
 
\t   <td>{{user.name}}</td> 
 
\t   <td>{{user.gender}}</td> 
 
\t   <td>{{user.email}}</td> 
 
\t   <td>{{user.address}}</td> 
 
\t   <td>{{user.phone}}</td> 
 
\t   <td> 
 
\t   <button class="btn btn-primary">Edit</button> 
 
\t   <button class="btn" name="btnDelete" ng-click="deleteData(user.email)" value="delete">Delete</button></td> 
 
\t   </tr> 
 
\t  </tbody> 
 
\t  </table> 
 
\t </div> 
 
    </div> 
 

 
    <!-- Modal --> 
 
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> 
 
    <div class="modal-dialog modal-lg" role="document"> 
 
    <div class="modal-content"> 
 
     <div class="modal-header"> 
 
     <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
 
     <h4 class="modal-title" id="myModalLabel">Modal title</h4> 
 
     </div> 
 
     <div class="modal-body"> 
 
     \t <div class="col-md-12"> 
 
\t \t  \t <form class="form form-horizontal"> 
 

 
\t \t  \t \t <div class="form-group hidden"> 
 
\t \t   \t \t <label for="name">id</label> 
 
\t \t   \t \t <input type="hidden" class="form-control" ng-model="id" id="name"> 
 
\t \t   \t </div> 
 
\t \t   \t <div class="form-group"> 
 
\t \t   \t \t <label for="name">Name</label> 
 
\t \t   \t \t <input type="text" class="form-control" ng-model="name" id="name"> 
 
\t \t   \t </div> 
 

 
\t \t   \t <div class="form-group"> 
 
\t \t   \t \t <label for="gender">Gender</label> 
 
\t \t   \t \t <input type="radio" id="gender" ng-model="gender" value="male">Male 
 
\t \t   \t \t <input type="radio" id="gender" ng-model="gender" value="female">Female 
 
\t \t   \t </div> 
 

 
\t \t   \t <div class="form-group"> 
 
\t \t   \t \t <label for="email">Email</label> 
 
\t \t   \t \t <input type="text" class="form-control" ng-model="email" id="email"> 
 
\t \t   \t </div> 
 

 
\t \t   \t <div class="form-group"> 
 
\t \t   \t \t <label for="address">Address</label> 
 
\t \t   \t \t <input type="text" class="form-control" ng-model="address" id="address"> 
 
\t \t   \t </div> 
 

 
\t \t   \t <div class="form-group"> 
 
\t \t   \t \t <label for="phone">Phone Number</label> 
 
\t \t   \t \t <input type="text" class="form-control" ng-model="phone" id="phone"> 
 
\t \t   \t </div> 
 

 
\t \t   \t <button type="button" class="btn btn-primary" ng-click="insertData()" type="submit">Insert</button> 
 
\t \t  \t </form> 
 
\t \t </div> 
 
     </div> 
 
     <div class="modal-footer"> 
 
     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
 
     
 
     </div> 
 
    </div> 
 
    </div> 
 
</div> 
 
</div> 
 
    
 
</body> 
 
</html>

コントローラ/ User.php

<?php 
defined('BASEPATH') OR EXIT ('DIRECT ACCESS IS NOT ALLOWED'); 

class User extends CI_Controller{ 
    public function index(){ 
     $this->load->view('user_view'); 
    } 

    public function user_list(){ 
     $data = $this->user_model->get_user_list(); 
     $this->output->set_content_type('application/json')->set_output(json_encode($data)); 
    } 

    public function insert_user(){ 
     //$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; 
     $_POST = json_decode(file_get_contents('php://input'),true); 
     $data = $_POST; 
     $this->user_model->insert_user_data($data); 
    } 

    public function delete_user(){ 
     $_POST = json_decode(file_get_contents('php://input'),true); 
     $email= $_POST['email']; 
     $this->user_model->delete_user_data($email); 
    } 
} 
?> 

モデル/ user_model.php

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 
class User_model extends CI_Model{ 
    protected $table_name; 

    public function __construct(){ 
     parent::__construct(); 
     $this->table_name = 'user'; 
    } 

    public function get_user_list(){ 
     return $this->db->from($this->table_name)->get()->result_array(); 
    } 

    public function insert_user_data($data){ 
     $this->db->insert($this->table_name, $data); 
    } 

    public function delete_user_data($email){ 
     $this->db->where('email',$email); 
     $this->db->delete('user'); 
    } 
} 
?> 

ネットワークの削除をクリックした後。

enter image description here

答えて

0

あなたがここに間違ってやっている事は、関数に引数として電子メールを渡し、ある、あなたはそれを利用されていません。

$scope.deleteData = function(email) { 


     $http.post('<?php echo site_url('index.php/user/delete_user');?>', 
      {'email':email}).error(function(){ 
       console.log("error"); 
      }) 
    } 
+0

ありがとうございますが、まだ同じです。ネットワーク - >リクエストペイロード - > {} プロパティがありません –

+0

@Jasjeet Singh:実際に私はちょっと、あなたがここでやっていない別のことは、サーバー側の問題か間違ったものがあるはずです。 – Jigar7521

+0

大丈夫です、解決したら解決します。ありがとうございました。 –

0

は、単一引用符

$http.post('<?php echo site_url("index.php/user/delete_user");?>', 
     {'email':$scope.email}).error(function(){ 
      console.log("error"); 
     }) 
内部の二重引用符を使用します
+0

ありがとうございますが、動作していません。 –

+0

あなたのブラウザで何らかのエラーチェックをしています。 – mahethekiller

+0

成功すると実行するために.errorを使用してください。 'code' .then(function(response){ alert(" success "); }); 'code' – mahethekiller