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 爬知乎登录之后内容加载不出来
  • ¥15 怎么用protues测量通频带
  • ¥15 zepelin使用sparkInterpreter 异常
  • ¥15 paho mqtt 接收不到消息
  • ¥15 函数r关于两个分量y,z方向上的图像,分开画r随y的图像,r随z的图像
  • ¥15 如何用Matlab求Pearcey函数的数值积分?
  • ¥15 一个简单的函数定义问题,输出结果是相反的,怎么回事呢?
  • ¥15 页面加载报错,不知道怎么处理
  • ¥20 vue-draggable-resizable选中状态保持
  • ¥15 flink cdc读取SqlServer2016