【问题标题】:Codeigniter - Ajax Return Data from Controller/Model questionCodeigniter - Ajax 从控制器/模型问题返回数据
【发布时间】:2011-08-04 20:50:36
【问题描述】:

这是我第一次尝试使用带有搜索输入框的 ajax。我的 ajax 工作正常,但是我不知道如何正确地从模型和控制器返回搜索数据。

控制器:“你找到了 twiiter ID”工作正常,但是 $num 没有通过

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        
        }               
    }
}

模型。

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;
    }
  }

控制器和模型的基础工作正常,最后我意识到我没有将数据从模型正确传递到控制器

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;       

});
});

jquery 工作得很好,警报 /firebug 告诉我我正在将数据传递给控制器​​。我只是不知道如何将变量数据的传递写入模型->控制器->视图

感谢阅读

【问题讨论】:

    标签: php jquery codeigniter search


    【解决方案1】:

    看起来您使用的变量超出了范围。

    在你的模型中你应该改变这个:

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

    到这样的事情:

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

    然后在你的控制器中改变这个:

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

    变成这样:

    echo "<h3>You found ".$query['count']." Twitter ID's</h3>"; 
    echo "<li class=\"list1\">".$query['twit_id']." - ".$query['query']."</li> ";
    

    【讨论】:

    • 谢谢icchanobot,这很有帮助。我很感激
    【解决方案2】:
    $data - array(
         $query,
         $num
         );
    

    你在这里打错了吗?应该是 $data = 数组吗? 此外,搜索函数中的变量 $num 既没有定义也没有返回。

    【讨论】:

    • 是的,这是我的错字。 $num 是 $num = $query->num_rows();在模型中。但是将 $query & $num 转换为变量时仍然存在同样的问题
    • 是的,$num 只在模型中,但它不会返回到搜索功能。所以你不能直接在搜索功能中使用 $num。
    • 我可以通过控制器将数据传递给视图,但我从未将“数据”转换为控制器中的变量。这是怎么做到的?
    猜你喜欢
    • 2017-02-06
    • 2012-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-15
    相关资源
    最近更新 更多