计划
我的计划是使用 JQUERY 从视图内的三个字段中检索输入。然后使用 AJAX 将该输入发送到控制器,然后控制器将该数据发送到模型。要用于检索数据,请将结果发送回 .js 文件,然后修改表格以在视图中显示数据。
问题
查看我的代码,看起来数据没有从 .js 文件发送到控制器。我认为这是因为数据库中的数据没有显示在修改后的表中。此外,当我在控制器中放置回显以发送回 .js 以触发警报时,以查看 AJAX 是否成功。什么都没发生。
我的 Javascript 代码
$('#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
我的控制器功能代码
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)));
}
我的模型功能代码
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;
}
问题
有谁知道我哪里出错了,我的问题的解决方法是什么???
谢谢