douzhan1963 2011-04-13 16:11
浏览 36
已采纳

Codeigniter - 从控制器/模型问题返回Ajax数据

This is my first foray into using ajax with a search input box. My ajax works fine however I do not know how to properly return the search data from the model and controller.

The controller: The "You have found twiiter ID's" works fine, however the $num does not come through

function search()
{         
    $this->form_validation->set_rules('twit', 'Search', 'required|trim|min_length[2]|max_length[25]');//check
    if ($this->form_validation->run() == TRUE) {
    $this->load->model('twit_model');
    $query = $this->twit_model->entity();
    if(!$query = $this->twit_model->entity())
    {  
    echo "The name does not exist";
        }else{     
        echo "<h3>You found $num Twitter ID's</h3>";    //this shows up in the view           
        echo "<li class=\"list1\">$twit_id - $name</li> ";   //the "-" shows up in the view        
        }               
    }
}

The model.

function entity()
{
    $twit = $this->input->post('twit');
    $this->db->select('id, name, state, party, twit_id, job');
    $this->db->like('name', $twit);
    $this->db->or_like('state', $twit);
    $this->db->or_like('party', $twit);
    $this->db->or_like('twit_id', $twit);
    $this->db->or_like('job', $twit);
    $query = $this->db->get('twit');
    $num = $query->num_rows();
    if($query->num_rows() > 0) {
         $data - array(
         $query,
         $num
         );
        return $data;
    }
  }

The basics of the controller and model work fine, I realize at the end of both I am not passing the data from the model to the controller correctly

The jquery

$(function() {
$('#display').hide();   
$('#name_error').hide(); 
    $('#submit').click(function() { // could be #form .submit

      var twit = $("#twitter_search").val();
        if (twit == "") {
          $("label#name_error").show();
          $("input#twitter_search").focus();
          return false;
        }  
var datastring = $('#form').serialize();         
$.ajax({
   url: "<?php echo site_url('twitter/search'); ?>",
   type: "POST",
   data: datastring, 
    success: function(msg) {
        $('#display').html(msg).show(3000);            
        }
    }); 
    return false;       

});
});

The jquery works just fine, alerts /firebug tell me I am passing the data to the controller. I just dont know how to write the passing of the variable data to the model->controller->view

Thanks for reading

  • 写回答

2条回答 默认 最新

  • doutang6819 2011-04-14 01:52
    关注

    It kinda just looks like you are using variables out of scope.

    in your model you should change this:

    $data - array(
         $query,
         $num
         );
    

    to something like this:

    $data = array('query' => $query, 'count' => $num, 'twit_id'=>$twit);
    

    then in your controller change this:

    echo "<h3>You found $num Twitter ID's</h3>";           
    echo "<li class=\"list1\">$twit_id - $name</li> ";  
    

    into something like this:

    echo "<h3>You found ".$query['count']." Twitter ID's</h3>"; 
    echo "<li class=\"list1\">".$query['twit_id']." - ".$query['query']."</li> ";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集