AngularJS | How to Send the Data of Json to databa

2019-05-19 07:08发布

问题:

I am trying to use Angular.

I am trying to Post Form of Angular, which have been done successfully, it is posting in JSON, But Problem is it only Post the input fields which have values, if i have NOT filled a textbox then it will NOT post that textbox value and fieldname, so how to know in controller which input fields have been posted.

Also how to send it to the model??

Here what i have done so far.

View.

<!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>

Controller:

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);
        }

here is the screenshot, i have filled three fields so it posted the values of three fields only.

回答1:

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);
        }


回答2:

i check like this :-

 if($this->input->get_post('male'))
    {
     // rest code goes here
    }


回答3:

Change your input name to match exactly like the database attribute(column) name. In that way you will not have to check anything:

$data = $this->input->post(null);    #get the post array
$this->db->insert('table', $ata);