Welcome Guest, Not a member yet? Register   Sign In
CI and Ajax request
#1

[eluser]SaminOz[/eluser]
I wonder if someone can help me understand the basics behind CI's url strings and jquery ajax requests?

I set up a page that takes entry for a new job on the site.
The url is example.com/controller/job[function] in the normal way.

When someone clicks on the date input an ajax call is initiated to see if a job already exists for this date. If it does/doesn't it returns a specially formatted job number.

The url for this action (sent via ajax) is example.com/controller/ajax_call

All works great. So I set up a new parameter to the job function, which is a job number - now if the parameter is present the controller loads the model and retrieves the job data from the db and populates variables on the page.

The uri for this action is example.com/controller/job/399[parameter]

I had assumed that when the input was clicked and the ajax call was run nothing would change - my understanding is/was that the http request is generated from the ajax code and nothing from the page is refreshed or used.

However: it seems that if my browser url has /399 appended the request will go via the /job function and not the ajax_call function, present the parameter, invoke the call on the db and generate an error on the page.

Since I don't understand ajax very well - I'm thinking I must be missing a significant part of the puzzle here!

JOB FUNCTION:
function jobs($job_ID = NULL)
{
//--- control view based on login admin_level
if($this->session->userdata['admin_level'] < 1)
$this->view_status($this->session->userdata['admin_level']);
//--- load up customers for scrolling list
$this->load->model('jobs');
$query = $this->jobs->load_customers();
$result = $query->result();
foreach($result as $value)
{
$customers_ID[] = $value->customers_ID;
$customer_name[] = $value->customer_name;
}
$data['customers'] = array_combine($customers_ID, $customer_name);
//--- load up principals for scrolling list
$data['principals'] = $this->jobs->load_principals();
//--- load up staff for scrolling list
$data['staff'] = $this->jobs->load_staff();
//--- load up stock for scrolling list
$data['stock'] = $this->jobs->load_stock();
$this->load->library('time');
/*job_number passed ----------------------------------------------------
| load up data from db to populate fields
*---------------------------------------------------------------------*/
if($job_ID)
{
$q = $this->jobs->load_job($job_ID);
$data['job_data'] = $q->row();
$data['job_data']->job_date = parse_date($data['job_data']->job_date);
//var_dump($data['job_data']);
//--- concatenate first and last_name to produce full_name for view
if(!empty($data['job_data']->first_name) || !empty($data['job_data']->last_name))
{
$data['job_data']->full_name = $data['job_data']->first_name. ' ' .$data['job_data']->last_name;
}
else
{
$data['job_data']->full_name = 'enter principal';
}

//--- test loading up staff
$q = $this->jobs->load_staff_used($job_ID);
$data['staff_used'] = $q->row();

}

/*Second--------------------------------------------------------------
| load local variables + helpers
*--------------------------------------------------------------------*/

$data['images'] = $this->images;
$data['scripts'] = FALSE;
$data['admin_level'] = $this->admin_level;
$data['period'] = date('l jS \of F Y');
$data['title'] = 'Dashboard &ndash; Job View';
$data['h1_title'] = 'Job View';
$data['start_time'] = $this->time->hours_of_day();
$data['atts'] = array('id' => 'job_submit');
/*Third---------------------------------------------------------------
| load views
*--------------------------------------------------------------------*/
$data['tab_setting'] = 'jobs';
$data['tabs'] = $this->load->view('dashboard/modules/dashboard_tabs', $data, TRUE);
$data['job_view'] = $this->load->view('dashboard/modules/job_view', $data, TRUE);
$this->load->view('dashboard/dashboard_jobs', $data);
}

AJAX FUNCTION:
function ajax_handler_jobs()
{
$date = trim($this->input->post('date'));
$this->load->model('jobs');
$job_number = $this->jobs->suffix($date);
sleep(4);
print trim($job_number);
}

Jquery Ajax call:
$.ajax({
type: 'POST',
url: 'ajax_handler_jobs',
data: {date: checkDate},
success: function(data) {
//--- remove the loading gif and replace with the job no.
Jobs.writeJobNo(data);
}
});
#2

[eluser]slowgary[/eluser]
Hi SaminOz,

It's much easier for people to help when you use [ code ] tags. Without them, it sure is a lot tougher to read through your post.


Original post below (now with syntax highlighting)
==================================================================

I wonder if someone can help me understand the basics behind CI's url strings and jquery ajax requests?

I set up a page that takes entry for a new job on the site.
The url is example.com/controller/job[function] in the normal way.

When someone clicks on the date input an ajax call is initiated to see if a job already exists for this date. If it does/doesn't it returns a specially formatted job number.

The url for this action (sent via ajax) is example.com/controller/ajax_call

All works great. So I set up a new parameter to the job function, which is a job number - now if the parameter is present the controller loads the model and retrieves the job data from the db and populates variables on the page.

The uri for this action is example.com/controller/job/399[parameter]

I had assumed that when the input was clicked and the ajax call was run nothing would change - my understanding is/was that the http request is generated from the ajax code and nothing from the page is refreshed or used.

However: it seems that if my browser url has /399 appended the request will go via the /job function and not the ajax_call function, present the parameter, invoke the call on the db and generate an error on the page.

Since I don't understand ajax very well - I'm thinking I must be missing a significant part of the puzzle here!

JOB FUNCTION:
Code:
function jobs($job_ID = NULL)
    {
    //--- control view based on login admin_level
        if($this->session->userdata['admin_level'] < 1)
            $this->view_status($this->session->userdata['admin_level']);
    //--- load up customers for scrolling list
        $this->load->model('jobs');
        $query = $this->jobs->load_customers();
        $result = $query->result();
        foreach($result as $value)
        {
            $customers_ID[] = $value->customers_ID;
            $customer_name[] = $value->customer_name;
        }
        $data['customers'] = array_combine($customers_ID, $customer_name);
    //--- load up principals for scrolling list
        $data['principals'] = $this->jobs->load_principals();
    //--- load up staff for scrolling list
        $data['staff'] = $this->jobs->load_staff();
    //--- load up stock for scrolling list
        $data['stock'] = $this->jobs->load_stock();
        $this->load->library('time');
        /*job_number passed ----------------------------------------------------
        |   load up data from db to populate fields
        *---------------------------------------------------------------------*/
        if($job_ID)
        {
            $q = $this->jobs->load_job($job_ID);
            $data['job_data'] = $q->row();
            $data['job_data']->job_date = parse_date($data['job_data']->job_date);
            //var_dump($data['job_data']);
    //--- concatenate first and last_name to produce full_name for view
            if(!empty($data['job_data']->first_name) || !empty($data['job_data']->last_name))
            {
                $data['job_data']->full_name = $data['job_data']->first_name. ' ' .$data['job_data']->last_name;
            }
            else
            {
                $data['job_data']->full_name = 'enter principal';
            }
        
    //--- test loading up staff
            $q = $this->jobs->load_staff_used($job_ID);
            $data['staff_used'] = $q->row();
                    
        }
        
        /*Second--------------------------------------------------------------
        |        load local variables + helpers
        *--------------------------------------------------------------------*/

        $data['images'] = $this->images;
        $data['scripts'] = FALSE;
        $data['admin_level'] = $this->admin_level;
        $data['period'] = date('l jS \of F Y');
        $data['title'] = 'Dashboard &ndash; Job View';
        $data['h1_title'] = 'Job View';
        $data['start_time'] = $this->time->hours_of_day();
        $data['atts'] = array('id' => 'job_submit');
        /*Third---------------------------------------------------------------
        |        load views
        *--------------------------------------------------------------------*/
        $data['tab_setting'] = 'jobs';
        $data['tabs'] = $this->load->view('dashboard/modules/dashboard_tabs',    $data, TRUE);
        $data['job_view'] = $this->load->view('dashboard/modules/job_view', $data, TRUE);
        $this->load->view('dashboard/dashboard_jobs', $data);
    }
AJAX FUNCTION:
Code:
function ajax_handler_jobs()
    {
        $date = trim($this->input->post('date'));
        $this->load->model('jobs');
        $job_number = $this->jobs->suffix($date);
        sleep(4);
        print trim($job_number);
    }
Jquery Ajax call:
Code:
$.ajax({
            type: 'POST',
            url: 'ajax_handler_jobs',
            data: {date: checkDate},
            success: function(data) {
            //--- remove the loading gif and replace with the job no.
                Jobs.writeJobNo(data);    
            }
        });
#3

[eluser]slowgary[/eluser]
Okay... I still had a hard time with your post, as there's so much code that doesn't pertain to the issue. Also, while it's good that you've been thorough attempting to explain the problem, it was tough to follow the plot.

That being said, it appears that you're having a problem where sometimes your ajax call works and sometimes it doesn't, depending on the URL. The reason you are having this problem is that the URL parameter in your $.ajax() call is relative to the current url. So you've got:
Code:
$.ajax({
     url: 'ajax_handler_jobs'
});

Which means that when you're on the page "example.com/controller/" and you make the ajax call, you're really calling "example.com/controller/ajax_handler_jobs", and if you visit "example.com/controller/jobs/" and make that same ajax call, you're really calling "example.com/controller/jobs/ajax_handler_jobs". The same is true if the current URL is "example.com/controller/jobs/399", which would be understood as "example.com/controller/jobs/399ajax_handler_jobs" by your browser.

I recommend that you ALWAYS use an absolute path in your ajax calls, just as you should in links on your site. ALWAYS ALWAYS... no excuses.

Code:
$.ajax({
     url: '/controller/ajax_handler_jobs'
});


I hope this helps.




Theme © iAndrew 2016 - Forum software by © MyBB