Home > OS >  Passing information from AJAX to controller class
Passing information from AJAX to controller class

Time:10-28

I'm currently passing values in my URL that I want to GET and insert in my controller class for filtering. My current URL looks something like this: http://localhost/reports/lists?source=Product1&status=4. Now to get the value of suppose source, I'm using the following code:

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

Now I want this status variable to go to my controller class so that I can use it as 1 of my parameters for my model class:

Full code: View Class:

<?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"},   
        ]
    });
  }

Controller Class:

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

Now here in my controller class I want that AJAX variable to be passed so that I can use it my model query.

Edit: I've now found out the problem was with my type variable. As of now I'm using type : 'POST' in my AJAX view. Now if I change that to get, I can see the searchParams.get('source') output, but the other data gets wrong since those need POST. So now how do I have 2 types GET and POST in my ajax code.

CodePudding user response:

  1. you don't use $p
  2. you don't pass $_GET - "<?php echo site_url(); ?>reports/loadLeads", it must be "<?php echo site_url(); ?>reports/loadLeads?status=xxx"
  3. you must use $this->input->post('...') instead direct access
  • Related