当我打开一个特定的客户表单时,我的url更改为如下内容:
http://localhost/proj/dashboard/customers/edit/1
如您所见,有一个带有ID的参数(在本例中为1)。现在,在调用ajax之前,我想在调用ajax OR时在控制器上获得这个ID。
目前,我的ajax如下:
$.post("ajax_edit", {name: _name, age: _age, function(aux){
console.log(aux);
});
不发送任何带有ID的参数。我希望我可以通过PHP得到它。
print_r($this->uri->segment_array());
/* output:
Array
(
[1] => dashboard
[2] => customers
[3] => edit
[4] => ajax_edit
) */
但不幸的是,这就是我所看到的。那么,我怎样才能得到这个值呢?
在你的route.php配置
$route['dashboard/customers/edit/(:num)'] = "customers/edit/$1";
在你的customer.php控制器
public function edit($user_id) {
$data['user_id'] = $user_id;
$this->load->view('NAME_OF_VIEW_FILE', $data);
}
视图文件
$.post("ajax_edit", {user_id: <?= $user_id ?>, name: _name, age: _age, function(aux){
console.log(aux);
});
我认为这是一个正确的方式,而不是使用会话或玩uri_segments
以另一种方式解决。虽然我确实认为这不是最好的选择,但我还是使用了会话变量。
那么在控制器函数中编辑:
public function edit(){
// all code
$this->session->set_userdata('obj_id', $this->uri->segment(4)); // which is the ID of the customer
}
然后在ajax_edit函数中我只需要调用会话项
public function ajax_edit(){
$id = $this->session->userdata('obj_id');
}