2013-05-31 95 views
2

我做對Live搜索一個項目使用jQuery阿賈克斯,PHP笨和MySQLLive搜索的

這裏是我的模型代碼:

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 
class Subjectmodel extends CI_Model { 
    public function __construct() { 
     parent::__construct(); 

    } 
    function getSubject($search){ 
     $this->db->select("SUB_ID,NAME"); 
     $whereCondition = array('NAME' =>$search); 
     $this->db->where($whereCondition); 
     $this->db->from('ix08s_subjects'); 
     $query = $this->db->get(); 
     return $query->result(); 
    } 
} 
?> 

這裏是我的控制器代碼:

<?php 
class Subject extends CI_Controller { 
    function __construct() { 
     parent::__construct(); 
     $this->load->model('SubjectModel'); 
    } 
    public function index(){ 
     $search= $this->input->post('search'); 
     $query = $this->SubjectModel->getSubject($search); 
     echo json_encode ($query); 
    } 
} 
?> 

,這是我的看法代碼:

<html> 
<head> 
<script type="text/javascript" language="javascript" src="http://somexyz/js/javascripts/plugin/jquery.js"></script> 
<script type="text/javascript" src="http://somexyz/js/javascripts/plugin/json2.js"></script> 
<script> 
    $(document).ready(function(){ 
     $("#search").keyup(function(){ 
     if($("#search").val().length>3){ 
     $.ajax({ 
      type: "post", 
      url: "http://localhost/ibps/index.php/subject", 
      cache: false,    
      data:'search='+$("#search").val(), 
      success: function(response){ 
       $('#finalResult').html(""); 
       var obj = JSON.parse(response); 
       if(obj.length>0){ 
        try{ 
         var items=[]; 
         $.each(obj, function(i,val){            
          items.push($('<li/>').text(val.NAME)); 
         }); 
         $('#finalResult').append.apply($('#finalResult'), items); 
        }catch(e) {  
         alert('Exception while request..'); 
        }  
       }else{ 
        $('#finalResult').html($('<li/>').text("No Data Found"));  
       }  

      }, 
      error: function(){      
       alert('Error while request..'); 
      } 
     }); 
     } 
     return false; 
     }); 
    }); 
</script> 
</head> 
<body> 
<div id="container"> 
<input type="text" name="search" id="search" /> 
<ul id="finalResult"></ul> 
</div> 
</body> 
</html> 

這是我的輸出:

[{ 「SUB_ID」: 「1」, 「NAME」: 「物理」},{ 「SUB_ID」: 「2」, 「NAME」: 「物理」}]

但是這應該是正確的輸出

http://jsfiddle.net/serigo1990/uShzQ/

得到的答案

回答

1

添加dataType: 'json'阿賈克斯設置

0

==查看頁面= =

<select class="itemName form-control" name="itemName"></select> 
    <input type="submit" name="search" value="search" > 




    $('.itemName').select2({ 
    ajax: { 
     url: '<?php echo base_url(); ?>/Controller_name/search', 
     dataType: 'json', 
     processResults: function (data) { 
     return { 
      results: data 
     }; 
     }, 
     cache: true 
    } 
    }); 

       ==Controller== 
function search(){ 
    $this->load->database(); 
    $this->db->like('field_name', $this->input->get("q")); 
    $this->db->select('id,field_name as text'); 
    $this->db->from('software'); 
    $res = $this->db->get()->result_array(); 
    echo json_encode($res); 
}