AngularJS |如何将Json的数据发送到Codeigniter中的数据库 [英] AngularJS | How to Send the Data of Json to database in Codeigniter

查看:141
本文介绍了AngularJS |如何将Json的数据发送到Codeigniter中的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Angular。

I am trying to use Angular.

我试图发布Angular的表单,这已经成功,它是在JSON中发布,但问题是它只发布具有值的输入字段,如果我有 NOT 填充一个文本框,那么它将发布该文本框值和字段名,因此如何知道在控制器中哪些输入字段已发布。

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

far。

查看。

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

这里是截图,我填写了三个字段,三个字段。

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

推荐答案

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

这篇关于AngularJS |如何将Json的数据发送到Codeigniter中的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆