4

I am using codeigniter for my project and I am stuck trying to figure this out.

I have some javascript that needs to perform an AJAX call to fetch some results based on a dropdown value that was selected.

function fetchLines(){
 $.ajax({
    url: baseURL + "resources/ajax.php?node=fetchLines",
    type: 'GET',
    cache: false,
    data: {
       lineType: 'business'
    },
    error: function(err) {
        alert(err.statusText);
    },
    success: function(data) {

        console.log(data);

    }

});
}

In this AJAX file, I am trying to include my controller and then access the function within it.

<?php
define('BASEPATH', "AJAX");
require_once('../application/controllers/Project.php');


switch($_REQUEST['node']){

    case 'fetchLines':
        $objLines = new Project();
        $objLines->fetchLines($_REQUEST['lineType']);
    break;

}

?>

My CI Controller then has a private function in it which I am trying to call to get the data I need:

private function fetchLines($lineType){
    $lines = $this->project_model->fetchLines($lineType);
    return $lines;
}

My goal here is to have an AJAX file or controller (if needed) be used for all my AJAX calls. It needs to be able to access a controller and return data.

With the current code above, I am getting the error: Class 'CI_Controller' not found in <b>C:\xampp\htdocs\blueprint\application\controllers \Project.php

Is there a better way to handle situations like this? I'm not an expert with OOP but some reading suggested something along these lines.

1
  • Hmm okay, thanks for the info. Is there a better way to approach this? I know I'm not the only one run into a situation like this. Commented Jul 31, 2015 at 1:29

3 Answers 3

1

why you are not sending this request to the controller method instead ?

        function fetchLines(){
         $.ajax({
            url: baseURL + "controller-name/method-name",
            type: 'GET',
            cache: false,
            data: {lineType: 'business'},
            error: function(err) {
                alert(err.statusText);
            },
            success: function(data) {

                console.log(data);

            }

        });
        }

NOTE and in controller you can access these values as

function method-name(){
    echo $this->input->get('lineType');
}
Sign up to request clarification or add additional context in comments.

Comments

0

Lets say my ajax file is in the controllers folder And I want to re-use my controllers, i would do it like this :

$this->load->library('../controllers/your_controller'); 
$this->your_controller->_some_method($data);        

Load the controller as library and used it like a library. Hope this helps.

Comments

0

You should put the code that listens to AJAX call in a controller function only. The way you are trying to do is not a good practice at all. If you want a method to be executed only if the request was an XHR i.e.AJAX request then use

if($this->input->is_ajax_request()){ //your code }else{ redirect(base_url()) }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.