如何从数据库中获取数据并在codeigniter中使用jquery / ajax在视图页面中显示?

时间:2015-07-31 09:11:35

标签: javascript jquery ajax codeigniter

更改月/年

<input type="button" id="mydate1" name="mydate1" value="<?php echo $dat;?>" class="monthYearPicker" />

按钮的值从monthyearpicker更改。我想从数据库中获取数据并在视图页面中以codeigniter中的javascript / jquery / ajax作为表格格式显示。

的javascript:

<script language="JavaScript">

                   function budgetpic()
                   {
                      var a= document.getElementById('mydate1').value;
                   }
                   </script> 

这个我的script.my控制器是

class Money_c extends CI_Controller
{
function selectallbudget($mydate)
    {
        $this->money_m->selectbudget($mydate);
    }}

我的模型文件是:

class Money_m extends CI_Model
{ 
function __construct() 
    {
          $this->load->database();
    }function selectbudget($mydate)
    {
        $query=$this->db->query("SELECT * FROM budget WHERE date='$mydate'");
          return $query->result();
    }}

1 个答案:

答案 0 :(得分:0)

这是解决方案,试试这个:

$('#mydate1').on('click', function () {

  var textBoxVal = $(this).val();
  $.ajax({
    type: 'POST',
    url: '<?php echo base_url();?>Money_c/selectallbudget', // link to CI function
    data: {
        val: $(this).val()
    },
    success: function (msg) {
        // populate data that returned from CI(yourFunction)  
        // here `msg` is a returned data from your controller
        // see console.log to see the data                     
        console.log(msg);
    }
  });

});

在CodeIgniter的selectallbudget中作为示例:

 public function selectallbudget(){
   $mydate= $this->input->post('val');
   // here you can fetching data from DB
   // then send it back
   // got data as an array and assign it into variable
   $sendMe = $this->money_m->selectbudget($mydate);
   // parse into json
   // this we send into ajax success
   echo json_encode($sendMe);
 }