douxin1956 2015-01-08 23:21
浏览 61
已采纳

Codeigniter ajax - 使用JQuery检索Codeigniter中的行不起作用

I'm investigating how to retrieve rows from database using Codeigniter and AJAX but it is not working, I followed the steps from here: How to get a row from database using ajax in codeigniter and nothing: here is what my code looks like:

table (user):

+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| id_user        | int(11)      | NO   | PRI | NULL    | auto_increment |
| name           | int(11)      | YES  |     | NULL    |                |
| information    | varchar(200) | YES  |     | NULL    |                |
+----------------+--------------+------+-----+---------+----------------+

controller (welcome.php):

function get_invoices()
    {

        $company = $_POST['pass'];

        $this->login_select->get_login($company);//model

    }

view(JQuery) once is loaded there is a form with one type text(named pass) and the other a submit:

function sendData(){

        var data = $('form').serialize();
        $('form').unbind('submit'); 

        $.ajax({
            url: '<?php echo base_url()?>index.php/welcome/get_invoices',
            type: 'POST',
            data: data,
            success: function(data, textStatus, xhr) {
                  $('div#message').html(data);
            }
        });  
    }

the model (login_select.php) here is the problem:

public function get_login($myid)
    {           
        $temp = array();
        $this->db->select("user.*");
        $query = $this->db->get_where('user', array('id_user'=>$myid));
        $temp= $query->row_array();
        echo $temp['name'];
        //return $temp;
    }

and finally I get:

A PHP Error was encountered

Severity: Notice

Message: Undefined index: name

Filename: models/login_select.php

Line Number: 41

How to resolve this?, because I'm going to need to recover not just a row but the whole row from a table. Thanks

  • 写回答

2条回答 默认 最新

  • dqsxsmi3704 2015-01-09 01:56
    关注

    I fixed it, but I don't know if it's a good practice:

    basically I create another view (everything printed here will be updated in my div via ajax):

    controller:

    function get_invoices()
        {
    
            $data['login2'] = $this->login_select->get_login($company);
            $this->load->view('ajaxtest', $data);///the trick
    
        }
    

    JQuery in the view is the same.

    the model (nothing weird here):

    public function get_login($myid)
        {
    
    
            $query = $this->db->get_where('user', array('id_user'=>$myid));
    
            return $query->result_array();
    
        }
    

    and the trick is create another view in this case ajaxtest.php:

    <?php
    
    
    echo sizeof($login2) . " ";
    
    foreach ($login2 as $login2_item):
    
        echo $login2_item["name"];
    
    endforeach;
    
    ?>
    

    If anyone needs this in the future.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大