将信息从AJAX传递到控制器类



我目前在我的URL中传递值,我想GET并插入到我的控制器类中进行过滤。我当前的URL看起来像这样:http://localhost/reports/lists?source=Product1&status=4。现在,为了获得source的值,我使用以下代码:

let searchParams = new URLSearchParams(window.location.search);
var status = searchParams.get('source');

现在我想让这个状态变量进入我的控制器类这样我就可以把它作为模型类的参数之一:

完整代码:视图类:

<?php  
$postd = json_encode(array_filter($post));
?>
<table id="item-list">
<tr> 
<th>Ref.No#</th>
<th>Source</th>
</tr>
</table>
<script>
$(document).ready(function() {
function sendreq(){
setpostdatas();cleartable();getleads();
}
var userrole = "<?php echo $this->session->userdata('clientrole')?>";
var slug = '<?php echo $slug?>';
var postd = '<?php echo $postd; ?>';
if( userrole > 1 && userrole != 5){
$('#item-list').DataTable({
"processing": true,
"stateSave": true,
"serverSide": true,
"ordering": false,
"ajax": {
url: "<?php echo site_url(); ?>reports/loadLeads",
data: {slug: slug, postdata: postd, status: status},
type : 'POST',
"dataSrc": function ( d ) {
d.myKey = "myValue";
if(d.recordsTotal == 0 || d.data == null){
$("#item-list_info").text("No records found");
$("#item-list_processing").css("display","none");
}
return d.data;
}
},
'columns': [
{"data": "id", "id": "id"},
{"data": "refno", "refno": "refno"},
{"data": "source", "source": "source"},   
]
});
}

控制器类:

public function loadLeads($p=''){
$leadsource = $this->input->get('status');
if(isset($_POST['postdata'])){
if($_POST['postdata'] != null && $_POST['postdata'] != 'null'){
$post=$_POST['postdata'];
}
$post = json_decode($post,true);
unset($post['slug']);
unset($post['page']);
$sort = $post['afsort'];
if($sort == "asc"){
$sortQ = 'l.updated_date asc,';
}else if ($sort == "desc"){
$sortQ = 'l.updated_date desc,';
}
}
$offset = (int)$_POST['start'] ;
$pstdatas = explode(',', $_POST['postdata']);
unset($pstdatas['item-list_length']);
if($this->session->userdata('clientrole') == 1 || $this->session->userdata('clientrole') == 5 ){
$content['leads']=$this->leads_model->get_pagination($_POST['length'],$offset,$where,'',false,$sortQ?$sortQ:'l.assign_status ='Unassigned' desc,',$all,$leadsource);          
}else{
$content['leads']=$this->leads_model->get_pagination($_POST['length'],$offset,$where,'',false,$sortQ?$sortQ:'l.assigned_date desc,',$all,$leadsource);
}

现在在我的控制器类中,我想要传递AJAX变量,以便我可以使用它作为模型查询。

编辑:我现在发现问题出在我的类型变量上。到目前为止,我在AJAX视图中使用type : 'POST'。现在,如果我将其更改为get,我可以看到searchParams.get('source')输出,但其他数据出错,因为那些需要POST。那么现在我如何在我的ajax代码中有两种类型GET和POST。

  1. 您不使用$p
  2. 你没有传递$_GET -"<?php echo site_url(); ?>reports/loadLeads",它必须是"<?php echo site_url(); ?>reports/loadLeads?status=xxx"
  3. 必须使用$this->input->post('...')而不是直接访问

最新更新