Welcome Guest, Not a member yet? Register   Sign In
Controller Method using jQuery Question
#1

[eluser]jtrainaldi[/eluser]
Maybe someone would be able to assist me in this question that has some more experience with Codeigniter and jQuery.

First, let me describe my application. I have a simple user checkin/out application log website that tracks when users enter and leave a computer lab (user enters in their ID into a Kiosk machine). When you click on one of the log records a jQuery dialog box opens up and uses the .load() jQuery function to populate the HTML form with the information from the database. When the "Update" or "Add" buttons are clicked I am using the .ajax() jQuery function to submit the data to the database. I have created several methods in my controller that are responsible for displaying the form, updating single rows, updating the entire log list, etc. For example, function ajax_loadForm(), function ajax_loadSingleRow(), etc.

I would like to make these ajax methods hidden so you can't call them by entering in them through the URL. I am aware that you can add an underscore to the beginning of the function that will make it hidden but it then prevents my .ajax() or .load() jQuery function from using them.

Thank you in advance for any help on this. If you have a better technique to go about this please let me know.
#2

[eluser]cideveloper[/eluser]
Try this

add

Code:
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');

to the bottom of /application/config/constants.php

then your controller will look like this

Code:
class do_ajax extends Controller {

    function do_ajax(){
        parent::Controller();    
    }
    
    function index(){
        //stuff
    }

    function ajax_loadForm(){
        if (IS_AJAX){
            //your code here
        } else {
            // an error view can be loaded here
        }
    }

    function ajax_loadSingleRow(){
        if (IS_AJAX){
            //your code here
        } else {
            // an error view can be loaded here
        }
    }
}
jquery sends a header HTTP_X_REQUESTED_WITH with a value of xmlhttprequest
#3

[eluser]pickupman[/eluser]
Or if you won't you have any urls load through a controller then take this another step by just using
Code:
class do_ajax extends Controller {

    function do_ajax(){
        parent::Controller();
        if(!IS_AJAX)
          redirect('/'); //we only will process ajax requests  
    }
#4

[eluser]Vheissu[/eluser]
Codeigniter 2.0 has an is_ajax_request() function inside of the input class.

So you can go:

if($this->input->is_ajax_request())
{
// Code here
}
#5

[eluser]jtrainaldi[/eluser]
Thank you everyone for your suggestions. I knew there was something out there.

Let me ask this question. In the opinion of the community would it be best practice to use this do_ajax controller for all my ajax functions?

For example, I mentioned one controller above for log records but I also have a controller for employees, students, etc. that each have several ajax functions that accomplish controller specific functions. Should I just combine all of the ajax functions from the other controllers into one do_ajax controller or would it be better to break them up depending on their intended controller purpose.

I love the structure that a MVC framework enforces and breaking up all of these controller specific functions seems wrong to me. Am I thinking too much about this?

Thanks again Codeigniter community
#6

[eluser]stormbytes[/eluser]
[quote author="Vheissu" date="1291979243"]Codeigniter 2.0 has an is_ajax_request() function inside of the input class.

So you can go:

if($this->input->is_ajax_request())
{
// Code here
}[/quote]


Ahhh cool beans! Didn't know that...

Now if I can only figure out how the heck to get my $.get() json params accepted by CI... life would be grand!




Theme © iAndrew 2016 - Forum software by © MyBB