簡體   English   中英

如何在codeigniter中使用jquery/ajax從數據庫中獲取數據並顯示在視圖頁面中?

[英]how to fetch data from database and display in view page using jquery/ajax in codeigniter?

更改月/年

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

按鈕的值從月年選擇器更改。 我想從數據庫中獲取數據並在視圖頁面中使用 codeigniter 中的 javascript/jquery/ajax 以表格格式顯示。

javascript:

<script language="JavaScript">

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

這是我的腳本。我的控制器是

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

這是解決方案,試試這個:

$('#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);
    }
  });

});

以 selectallbudgetof CodeIgniter 為例:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM