2

我正在尝试使用 Angular。

我正在尝试发布已成功完成的 Angular 表单,它以 JSON 格式发布,但问题是它只发布具有值的输入字段,如果我没有填写文本框,那么它不会发布该文本框值和fieldname,所以如何在控制器中知道哪些输入字段已发布。

另外如何将其发送到模型?

这是我到目前为止所做的。

看法。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
<head>
    <link href="<?php echo base_url(); ?>/styles/formcss.css" rel="stylesheet" type="text/css"/>
    <script src="<?php echo base_url(); ?>/scripts/angular.min.js"></script>
</head>
    <body ng-app>
    <div ng-controller="controller">
    <form>
    <label for="name">UserID:</label>   <input type="text" name="UserID" ng-model="user.UserID" value=""><br>
    <label for="UserName">Name:</label>   <input type="text"  name="UserName" ng-model="user.UserName" value=""><br>
    <label for="rollno">In Game Name:</label>   <input type="text" name="GameName" ng-model="user.InGameName" value="" ><br>
    <label for="father">CNIC Number:</label>   <input type="number" name="Cnic" ng-model="user.Cnic" value="" ><br>
    <label for="age">Age:</label>  <input name="Age" ng-model="user.Age" type="number" value="" ><br>
    <label for="email1">Email:</label>   <input name="Email" ng-model="user.Email" type="email" value="" ><br>
    <label for="email2">Res enter Email:</label>   <input id="email2" type="email" name="email2" value=""  ><br>
    <label for="pass1">Password:</label>  <input id="pass1" type="password" name="Pass" ng-model="user.Password" value=""><br>
    <label for="pass2">Re enter Password:</label>  <input id="pass2" type="password" name="pass2" value="" ><br>
    <label  style="margin-bottom:-10px; "  for="male">Male</label><input class="gen" ng-model="user.Gender" id="male" type="radio" name="g" value="Male"><br>
    <label  style="margin-bottom:15px;" for="female">Female</label><input class="gen" ng-model="user.Gender" id="female" type="radio" name="g" value="Female"><br>
    <label class="dis"  for="about" >About:</label> <textarea rows="5" cols="40" id="about" type="text" value=""></textarea><br>
    <button style="margin-left: 225px;"  ng-click="save()" >Save</button>
    </form>
    </div>
    <script>
       function controller ($scope,$http)
       {
           var angularform="<?php echo base_url(); ?>index.php/datacon/angform"
           $scope.save=function()
           {
            $http.post(angularform, $scope.user);
           }
       }
    </script>
    </body>
</html>

控制器:

public function angform()
    {
        $get=file_get_contents('php://input');
        $json_get=json_decode($get);
        foreach($json_get as $key=>$value){
         // i think something it has to do here.
        }

        $this->load->model('datamodel');
        $this->datamodel->insert($data);
        }

这是屏幕截图,我已经填写了三个字段,所以它只发布了三个字段的值。 在此处输入图像描述

4

3 回答 3

2
public function angform()
    {
        $get=file_get_contents('php://input');
        $json_get=json_decode($get);
        foreach($json_get as $key=>$value){              
         $data[$key]=$value; //Worked For Me.
        }

        $this->load->model('datamodel');
        $this->datamodel->insert($data);
        }
于 2013-07-12T10:47:03.950 回答
0

更改您的input名称以完全匹配数据库attribute(column)名称。这样您就不必检查任何内容:

$data = $this->input->post(null);    #get the post array
$this->db->insert('table', $ata);
于 2013-07-12T08:02:59.240 回答
0

我这样检查:-

 if($this->input->get_post('male'))
    {
     // rest code goes here
    }
于 2013-07-12T07:07:44.227 回答