我有以下代碼:如何獲取通過ajax發佈到控制器的變量的值?
$('.pPage').click(function(){
var recordsPerPage = $(this).attr('id');
$.ajax({
type: "POST",
dataType: 'json',
url: "backOfficeUsers/displayAllUsers",
data: { value: recordsPerPage }
}).done(function(msg) {
alert("Data Saved: " + msg);
});
});
我需要操作recordsPerPage變量在我的控制器。我如何接受我的控制器中的這個變量的值,並在我的PHP中使用它?
這裏是控制器代碼:
public function displayAllUsers()
{
$currentUser = $this->isLoggedIn();
$this->load->model('backOfficeUsersModel');
$this->db->order_by('userid');
$myrec = $this->input->post('value');
$recordsPerPage = $myrec;
$limit = $recordsPerPage;
$offset = 3;
$offset = $this->uri->segment(3);
$this->db->limit($limit, $offset);
$data['users'] = $this->backOfficeUsersModel->get();
$totalresults = $this->db->get('back_office_users')->num_rows();
//initializing & configuring paging
$this->load->library('pagination');
$config['base_url'] = site_url('/backOfficeUsers/displayAllUsers');
$config['total_rows'] = $totalresults;
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$config['full_tag_open'] = '<div class="dataTables_paginate paging_bootstrap pagination"><ul>';
$config['full_tag_close'] = '</ul></div>';
$config['cur_tag_open'] = '<li><a href=# style="color:#ffffff; background-color:#258BB5;">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
$data['main_content'] = 'bousers/users';
$data['title'] = 'Back Office Users';
$errorMessage = FALSE;
$this->load->vars($data,$errorMessage);
$this->load->vars($currentUser);
$this->load->view('backOffice/template');
} // end of function displayAllUsers
可能不是傳統的CI,但'$ _ POST [ 'value']'應該可以工作 – Phil
不起作用。 $ _post ['value']打印錯誤 – Zoran
PHP變量是大小寫敏感的。 $ _ POST!== $ _ POST。菲爾從非傳統的CodeIgniter角度來看是正確的。從CodeIgniter的角度來看,相當於'$ this-> input-> post(「value」);' –