在使用AJAX的codeigniter中从数据库检索数据 [英] Retrieving Data from a database in codeigniter using AJAX

查看:133
本文介绍了在使用AJAX的codeigniter中从数据库检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

计划



我得到的计划是使用JQUERY从视图内的三个字段检索输入。然后使用AJAX将该输入发送到控制器,控制器反过来将该数据发送到模型。要用于检索数据,将结果发送回.js文件,然后修改表以显示视图中的数据。



问题



看看我的代码,看起来不是数据没有从.js文件发送到控制器。我认为这是因为数据库中的数据没有显示在修改表中。此外,当我把一个回声内部的控制器发送回.js触发一个警报,看看AJAX是否成功。没有任何反应。



我的Javascript代码

  $('#bttnSubmit')。click(function(){
//从视图中收集输入并将它们放入变量
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){
$(' ).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>');
});
} //成功结束
})//结束AJAX方法
} else {
alert(你需要先选择一个输入!
} // if语句结束
}); //结束点击函数

我的控制器函数代码

 函数get_invoices(){
//使用_POST方法检索.js文件发送的数据
$ company = $ _POST ['company'];
$ dateFrom = $ _POST ['dateFrom'];
$ dateTo = $ _POST ['dateTo'];

//加载invoice_model
//初始化JSON标头
//使用从.js文件发送的参数编码响应,并将其发送回.js文件
$ 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)));
}

我的模型函数代码 b
$ b

 函数get_invoices($ company,$ dateFrom,$ dateTo){

//查询从数据库中检索数据
//发送回控制器以填充到表中

$ ONEDB = $ this-> load-> database('ONEDB',TRUE);
$ ONEDB-> select('InvoiceID,CompanyName,InvRef,InvDate,InvTotal,SageReference');
$ ONEDB->其中('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;

}

问题

有人知道我在哪里出了问题,解决我的问题是什么?



感谢

解决方案

确定,让我们从头开始。
1)在您的JS上,在之后发出警告:

  var company = $('#client:selected' ).val(); 
var dateFrom = $('#dateFrom')。val();
var dateTo = $('#dateTo')。val();
ALERT(company,dateFrom,dateTo);

查看变量是否有数据。如果是,继续。
2)检查url。在浏览器上粘贴您的网址,看看控制器是否真的被调用。



3)将您的ajax调用更改为jquery,它更简单。还要在您的成功代码中添加警报。

  var url =http:// ... 
var dataToSend = {company:company,dateFrom:dateFrom .....};
$ .post(url,dataToSend,function(data){

ALERT(data);

});

4)在PHP控制器上,测试是否正在接收数据:

  function get_invoices(){
$ company = $ this-> input-> post('company');
$ dateFrom = $ this-> input-> post('dateFrom');
$ dateTo = $ this-> input-> post('dateTo');

echo $ company;
echo $ dateFrom;
echo $ dateTo;

}

5)你会得到什么?


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

解决方案

ok, lets start from scratch. 1)On your JS, place an alert after:

var company = $('#client :selected').val();
    var dateFrom = $('#dateFrom').val();
    var dateTo = $('#dateTo').val();
ALERT(company, dateFrom, dateTo );

see if the variables have data. If so, proceed. 2)check the url. paste your url on the browser and see if the controller is actually being called. if it isnt, fix that url.

3)change your ajax call to jquery, its simpler.Also add an alert in your success code.

var url = "http://...";
var dataToSend= {company: company, dateFrom: dateFrom.....};
    $.post(url, dataToSend, function(data) {

       ALERT(data);

    });

4) on PHP controller, test if data is being received like so:

function get_invoices() {
  $company = $this->input->post('company');
  $dateFrom= $this->input->post('dateFrom');
  $dateTo= $this->input->post('dateTo');

  echo $company; 
  echo $dateFrom; 
  echo $dateTo;

}

5) what do you get?

这篇关于在使用AJAX的codeigniter中从数据库检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆