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.

    点赞 打赏 评论
  • dsf487787 2015-01-08 23:26

    in login_select.php, $temp may not equal what you think it does.

    $temp['name'] doesnt exist and you're trying to use it so it throws an undefined index error. Make sure that $temp['name'] exists before using it via something like

    return isset($temp['name']) ? $temp['name'] : false;
    

    Or run die(var_dump($temp)) there to see whats going on.

    点赞 打赏 评论

相关推荐 更多相似问题