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

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

问题描述

我试图用棱角分明。

我想要的角度发表形式,它已经成功地完成的,它是在JSON发布,但问题是,它只能帖子里面有值的输入字段,如果我有不会填补了文本框,然后将不会发表的文本框的值和字段名,所以如何在控制器知道输入字段已张贴这。

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

下面是我迄今所做的。

查看。

 <!DOCTYPE HTML>
< HTML的xmlns =htt​​p://www.w3.org/1999/html>
< HEAD>
    <链接HREF =<?PHP的回声BASE_URL();?> /styles/formcss.css的rel =stylesheet属性类型=文/ CSS/>
    < SCRIPT SRC =<?PHP的回声BASE_URL();?> /scripts/angular.min.js>< / SCRIPT>
< /头>
    <机身NG-应用>
    < D​​IV NG控制器=控制器>
    <形式为GT;
    <标签=名与GT;用户名:​​其中; /标签> <输入类型=文本名称=用户名NG模型=user.UserIDVALUE =>< BR>
    <标签=用户名>名称:LT; /标签> <输入类型=文本名称=用户名NG模型=user.UserNameVALUE =>< BR>
    <标签=rollno>在游戏名称:< /标签> <输入类型=文本名称=GameNameNG模型=user.InGameNameVALUE =>< BR>
    <标签=父亲> CNIC编号:LT; /标签> <输入类型=数字NAME =网络中心NG模型=user.CnicVALUE =>< BR>
    <标签=年龄>年龄:< /标签> <输入名字=时代NG-模式=user.Age类型=数字值=>< BR>
    <标签=EMAIL1>电子邮件:< /标签> <输入名字=电子邮件NG-模式=user.EmailTYPE =电子邮件值=>< BR>
    <标签=EMAIL2> RES填写邮箱:< /标签> <输入ID =EMAIL2TYPE =电子邮件NAME =EMAIL2VALUE =>< BR>
    <标签=PASS1>密码:LT; /标签> <输入ID =PASS1TYPE =密码NAME =通行证NG模型=user.password的价值=>< BR>
    <标签=PASS2>再输入密码:< /标签> <输入ID =PASS2TYPE =密码NAME =PASS2VALUE =>< BR>
    <标签样式=保证金底:-10px;为=男性>男< /标签><输入类=根NG模型=user.GenderID =男类型= 单选NAME =G值=男>< BR>
    <标签样式=保证金底:15px的;对于=女>女< /标签><输入类=根NG模型=user.GenderID =女类型=电台NAME =G值=女&GT ;< BR>
    <标签类=DIS的关于GT&=;简介:< /标签> < textarea的行数=5COLS =40ID =约类型=文本值=>< / textarea的>< BR>
    <按钮样式=保证金左:225px; NG-点击=保存()>保存< /按钮>
    < /表及GT;
    < / DIV>
    <脚本>
       功能控制器($范围,$ http)的
       {
           VAR angularform =<?PHP的回声BASE_URL();?>的index.php / DATACON / angform
           $ scope.save =功能()
           {
            $ http.post(angularform,$ scope.user);
           }
       }
    < / SCRIPT>
    < /身体GT;
< / HTML>

控制器:

 公共职能angform()
    {
        $得到=的file_get_contents('PHP://输入');
        $ json_get = json_de code($获得);
        的foreach($ json_get为$关键=> $值){
         //我认为有些事情它在这里做。
        }        $这个 - >负载>模型('数据模型');
        $这个 - > datamodel->插入($的数据);
        }

下面是截图,我已经填写三个字段,因此贴只有三个字段的值。


解决方案

 公共职能angform()
    {
        $得到=的file_get_contents('PHP://输入');
        $ json_get = json_de code($获得);
        的foreach($ json_get为$关键=> $值){
         $数据[$关键] = $价值; //为我工作。
        }        $这个 - >负载>模型('数据模型');
        $这个 - > datamodel->插入($的数据);
        }

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.

解决方案

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天全站免登陆