简体   繁体   中英

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

Change Month/Year

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

the value of button changes from monthyearpicker. I want to get data from database and display in view page as a table format using javascript/jquery/ajax in codeigniter.

javascript:

<script language="JavaScript">

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

this my script.my controller is

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

and my model file is:

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

Here is the solution, try this:

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

});

In selectallbudgetof CodeIgniter as example :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM