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条)

报告相同问题?

悬赏问题

  • ¥15 Chrome Manifest扩展引用Ajax-hook库拦截请求失败
  • ¥15 用Ros中的Topic通讯方式控制小乌龟的速度,走矩形;编写订阅器代码
  • ¥15 LLM accuracy检测
  • ¥15 pycharm添加远程解释器报错
  • ¥15 如何让子窗口鼠标滚动独立,不要传递消息给主窗口
  • ¥15 如何能达到用ping0.cc检测成这样?如图
  • ¥15 关于#DMA固件#的问题,请各位专家解答!
  • ¥15 请问华为OD岗位的内部职业发展通道都有哪些,以及各个级别晋升的要求
  • ¥20 微信小程序 canvas 问题
  • ¥15 系统 24h2 专业工作站版,浏览文件夹的图库,视频,图片之类的怎样删除?