【问题标题】:Post edit form data and get it in controller using angularjs发布编辑表单数据并使用 angularjs 在控制器中获取它
【发布时间】:2018-08-06 11:10:43
【问题描述】:

我想使用 angularjs 将编辑表单数据发布到 codeigniter 控制器

这是我收集和发布表单数据的 angularjs 控制器

  $scope.editlist = function(id){
                $http({
                    method:'post',       
  url:'http://localhost/Angular_demo/admin/index.php/welcome/get_edit_data/'+ 
                id
                }).then(function success(response){
                    $scope.sid = parseInt(response.data[0]['id']);
                    $scope.firstname = response.data[0]["first_name"];
                    $scope.lastname = response.data[0]['last_name'];
                    $scope.email = response.data[0]['email'];                        
                }); 
            }

            $scope.saveEdit = function(id,firstname,lastname,email){
                $http({
                    method:'post',
                    data:'id='+ id + '&first_name='+firstname+ 
                    '&last_name='+lastname+'&email='+email,

      url:'http://localhost/Angular_demo/admin/index.php/welcome/update_data'                         
                }).then(function success(response){
                    console.log(response.data);
                });

            }

我在哪里绑定和发布表单数据的视图

       <form name="editItem" class="form-horizontal">
      <input ng-model="sid" type="hidden" placeholder="Name" name="name" 
      class="form-control" />          
      <div class="form-group"> 
          <label for="inputEmail3" class="col-sm-2 control-label">First 
          Name</label>
          <div class="col-sm-10"> 
              <input type="text" class="form-control" ng-model="firstname"  
          id="inputEmail3" placeholder="First name">
          </div> 
      </div>
      <div class="form-group"> 
          <label for="inputEmail3" class="col-sm-2 control-label">Last 
          Name</label>
          <div class="col-sm-10"> 
              <input type="text" class="form-control" ng-model="lastname" 
          id="inputEmail3" placeholder="Last Name">
          </div> 
      </div>
      <div class="form-group"> 
          <label for="inputEmail3" class="col-sm-2 control- 
          label">Email</label>
          <div class="col-sm-10"> 
              <input type="text" class="form-control" ng-model="email" 
          id="inputEmail3" placeholder="Email">
          </div> 
      </div>        

      <div class="form-group"> 
          <div class="col-sm-offset-2 col-sm-10">
              <button type="button" class="btn btn-default" data- 
              dismiss="modal">Close</button>
                <button type="submit" ng-disabled="editdata.$invalid" 
       class="btn btn-primary create-crud" data-dismiss="modal" ng- 
       click="saveEdit(sid,firstname,lastname,email)">Submit</button>                  
          </div> 
      </div>
  </form>

这是我想要获取表单数据的 codeigniter 控制器

  public function update_data(){
  $requests = json_decode(file_get_contents('php://input'), TRUE);
  $ids= array('id' =>$requests['id']);
  echo json_encode($ids);
  }

当我提交表单数据时,我的控制器中会显示 null,所以请帮助如何在控制器中获取表单数据。

【问题讨论】:

    标签: php angularjs codeigniter


    【解决方案1】:
     $scope.saveEdit = function(id,firstname,lastname,email){
                $http({
                    method:'post',
                    data:'id='+ id + '&first_name='+firstname+ 
                    '&last_name='+lastname+'&email='+email,
    
      url:'http://localhost/Angular_demo/admin/index.php/welcome/update_data'                         
                }).then(function success(response){
                    console.log(response.data);
                });
    
            }
    

    应该是这样的:-

     $scope.saveEdit = function(id,firstname,lastname,email){
                $http({
                    method:'post',
                    data:{"id": id,"first_name":firstname, 
                    "last_name":lastname,"email":email}
    
      url:'http://localhost/Angular_demo/admin/index.php/welcome/update_data'                         
                }).then(function success(response){
                    console.log(response.data);
                });
    
            }
    

    【讨论】:

      【解决方案2】:

      由于 codeigniter 代码使用json_decode,$http 服务应该使用 JavaScript 对象来发布数据:

      $scope.saveEdit = function(id,firstname,lastname,email) {
          var url = 'http://localhost/Angular_demo/admin/index.php/welcome/update_data';                         
          var data = {
            id: id,
            first_name: firstname,
            last_name: lastname,
            email: email,
          };
          $http.post(url, data
           ).then(function success(response){
              console.log(response.data);
          });
      }
      

      AngularJS 框架会自动将 JavaScript 对象编码为 JSON 字符串。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-11
        • 1970-01-01
        • 1970-01-01
        • 2014-11-10
        • 1970-01-01
        相关资源
        最近更新 更多