The Plan
The plan I've got is to use JQUERY to retrieve input from three fields inside the view. Then use AJAX to send that input to the controller, which in turn sends that data the model. To be used to retrieve data, send the result back to the .js file and then amend a table to display the data inside the view.
The Problem
Looking through my code, it doesn't look like the data isn't being sent from the .js file to the controller. I think this because the data from the database isn't being displayed inside the amended table. Also, when I put an echo inside the controller to be sent back to the .js to trigger an alert, to see if the AJAX was successful. Nothing happens.
My Javascript Code
$('#bttnSubmit').click(function() {
// Gather the input from the view and place them into variables
var company = $('#client :selected').val();
var dateFrom = $('#dateFrom').val();
var dateTo = $('#dateTo').val();
if (company != "") {
var post_url = "http://localhost/ProjectSage/index.php/site/members_area";
$.ajax ({
type: "POST",
url: post_url,
cache: false,
data: "company=" + company + "&dateFrom=" + dateFrom + "&dateTo=" + dateTo,
success: function(invoices) {
$.each(invoices, function(InvoiceID, CompanyName, InvRef, InvDate, InvTotal, SageReference){
$('.invoice_tbody').append('<tr>');
$('.invoice_tbody').append('<td class="invoice_td">' + InvoiceID + '</td>');
$('.invoice_tbody').append('<td class="invoice_td">' + CompanyName + '</td>');
$('.invoice_tbody').append('<td class="invoice_td">' + InvRef + '</td>');
$('.invoice_tbody').append('<td class="invoice_td">' + InvDate + '</td>');
$('.invoice_tbody').append('<td class="invoice_td">' + InvTotal + '</td>');
$('.invoice_tbody').append('<td class="invoice_td">' + SageReference + '</td>');
$('.invoice_tbody').append('</tr>');
});
} // End of success
}) // End of AJAX method
} else {
alert("You need to select an input first!!!");
} // End of if statement
}); // End of click function
My Controller Function Code
function get_invoices() {
// Retrieve the data sent from the .js file using _POST method
$company = $_POST['company'];
$dateFrom = $_POST['dateFrom'];
$dateTo = $_POST['dateTo'];
// Load invoice_model
// Initialise the JSON header
// Encode the response using the parameters sent from the .js file and send it back to the .js file
$this->load->model('invoice_model');
header('Content-Type: application/x-json; charset=utf-8');
echo(json_encode($this->invoice_model->get_invoices($company, $dateFrom, $dateTo)));
}
My Model Function Code
function get_invoices($company, $dateFrom, $dateTo) {
// Query to retrieve data from database
// Sent it back to the controller to be populated into a table
$ONEDB = $this->load->database('ONEDB', TRUE);
$ONEDB->select('InvoiceID, CompanyName, InvRef, InvDate, InvTotal, SageReference');
$ONEDB->where('ClientID', $company);
$ONEDB->where('InvDate >=', $dateFrom);
$ONEDB->where('InvDate <=', $dateTo);
$ONEDB->join('Supplier', 'Supplier.SupplierName = InvDetail.InvSupplier');
$query = $ONEDB->get('InvDetail');
$result = $query->result();
return $result;
}
Question
Does anybody know where I have gone wrong and what the fix to my problem is???
Thanks