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