使用codeigniter和ajax将textbox的值插入数据库

时间:2022-10-06 15:59:46

I add AJAX code into view part of codeigniter to grab value of input and send it to the controller:

我将AJAX代码添加到codeigniter的视图部分以获取输入值并将其发送到控制器:

$(document).ready(function(){

$("#1").click(function(){

var res = $('#name').attr("value");

$.ajax({

type:"POST",

url:'user/ctrl',

data:{'res':res},

success:function(result){

}

});

});

});

In the controller part I added

在我添加的控制器部分

class User extends CI_Controller {


    public function ctrl()

    {

        if($_POST['res']=="yes"){

        $res=0;

        $this->load->model("insert");

        $s=$this->insert->name($res);


        }

        else if($_POST['res']=="No"){

        $res=1;

        $this->load->model("insert");

        $s=$this->insert->name($res);

        }


    }    


}

Now I don't know what I should write into the model part to insert value of $res into the database.

现在我不知道我应该写入模型部分以将$ res的值插入数据库。

1 个解决方案

#1


0  

in your controller u need put this for recieve data from ajax

在你的控制器中你需要把它用于从ajax接收数据

$res = $this->input->get_post('res', TRUE);

then...

class User extends CI_Controller {

public function User(){
    parent::__construct();
    $this->load->model('insert','model');
}

public function ctrl(){
    $res = $this->input->get_post('res', TRUE);
    $data = $this->model->functionForInsert($res);
}

#1


0  

in your controller u need put this for recieve data from ajax

在你的控制器中你需要把它用于从ajax接收数据

$res = $this->input->get_post('res', TRUE);

then...

class User extends CI_Controller {

public function User(){
    parent::__construct();
    $this->load->model('insert','model');
}

public function ctrl(){
    $res = $this->input->get_post('res', TRUE);
    $data = $this->model->functionForInsert($res);
}