Jump to content

Passing information from AJAX to controller class


KSI

Recommended Posts

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,
          "createdRow": function( row, data, dataIndex){
            $(row).has( "div.overdueupdate" ).css('background-color','#FFC7CE');
              },
        "ajax": {
            url: "<?php echo site_url(); ?>reports/loadLeads",
            data: {slug: slug, postdata: postd},
            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.

Link to comment
Share on other sites

None of what you've given us makes sense given what you say the problem is. Assuming the line of code you say you're using:

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

actually is in your code - it doesn't appear in any of the code you posted - you still don't send the variable 'status' (which theoretically includes the value of the URL parameter 'source', not 'status' as the variable name implies) to $site_url()/reports/loadLeads. Also, you need to send an AJAX call to a php script (that ends in .php) and from there you can call a function - you can't call the actual php function from the URL string. And even if it did work that way, your loadLeads() method only accepts one parameter (which, admittedly, it doesn't seem to actually use), but your JS data object contains two.

There's more beyond this, but quite honestly you need to take a step back and pick up some basics. Learn to walk before you run - I remember learning AJAX back in the day and it made no sense whatsoever until I'd done enough base-level work to actually get it. The penny dropped and it hasn't been a problem since.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.