CodeIgniter結合Ajax編寫搜索功能。

須要prototype.js支持。 function_search.js window.onload = function () {  new Ajax.Autocompleter("function_name", "autocomplete_choices", base_url+'application/ajaxsearch/', {});  $('function_search_form').onsubmit = function () {   inline_results ();   return false;   } } function inline_results () {  new Ajax.Updater ("function_description", base_url+'application/ajaxsearch', {method:'post', postBody:'description=true&function_name='+$F('function_name')});  $('function_description').hide();  new Effect.Appear('function_description'); } 控制器: class Application extends Controller {  function Application()  {   parent::Controller();   $this->load->model('function_model');  }    function index()  {   $data['title'] = "Code Igniter Sample Application";   $data['extraHeadContent'] = '';   $this->load->view('application/index', $data);  }  function ajaxsearch ()  {   $searchTerm = $this->input->post('function_name');   $description = $this->input->post('description');   echo $this->function_model->getSearchResults($searchTerm, $description);  }    function search()  {   $data['title'] = "Code Igniter Sample Application Results";   $searchTerm = $this->input->post('function_name');   $data['search_results'] = $this->function_model->getSearchResults($searchTerm);   $this->load->view('application/search', $data);  } } ?> 模塊: class Function_model extends Model {  function Function_model()  {   parent::Model();  }  function getSearchResults ($function_name, $descriptions = TRUE)  {   $this->db->like('function_name', $function_name);   $this->db->orderby('function_name');   $query = $this->db->get('functions');   if ($query->num_rows() > 0) {    $output = '';    foreach ($query->result() as $function_info) {     if ($descriptions) {      $output .= '' . $function_info->function_name . '';      $output .= $function_info->function_description . '';     } else {      $output .= '' . $function_info->function_name . '';     }    }    $output .= '';    return $output;   } else {    return 'Sorry, no results returned.';   }  } } ?> 視圖: $this->load->view("header"); $this->load->view("footer"); ?>
相關文章
相關標籤/搜索