2013-03-06 113 views
0

我想建立一个窗体上的自动完成输入框,虽然一切似乎工作我没有数据传递到输入框。 Firebug报告成功,但没有。我想知道是否有人可以看我的代码,看看是否有任何可能导致它的明显错误。Codeigniter 2自动完成从数据库

脚本是:

(function($){ 
    $("#town").autocomplete({ 
     source :"drivers/driver_gettown", 
     minLength : 3, 
     dataType:'JSON', 
     type:'POST' 
    }); 
})(jQuery); 

输入框:

<div class="div"> 
     <input name="town" id="town" type="text" class="txtSelect input required" value="<?php echo set_value('town'); ?>" /> 
     <?php echo form_error('town'); ?> 
    </div> 

型号是:

class Driver_model extends CI_Model 
{ 
    public function __construct() { 
     // Load the Database 
     parent::__construct(); 
     $this->load->database(); 
    } 


    function driver_get_towns($q) 
    { 
     // Get a list of Towns 
     // Search for row "place_name" from Table called "tbk_towns" 
     $this->db->select('place_name'); 
     $this->db->like('place_name', $q); 
     $query = $this->db->get('tbk_towns'); 
     if($query->num_rows > 0) 
     { 
      foreach ($query->result_array() as $row) 
      { 
       //build an array for the towns 
       $row_set[] = htmlentities(ucfirst($row['place_name'])); 
      } 
      //format the array into json data 
      // header('Content-Type: application/x-json; charset=utf-8'); 
      // echo json_encode($row_set); 
      $this->output 
        ->set_content_type('application/json') 
        ->set_output(json_encode($row_set)); 
     } 
    } 
} 

和最后控制器:

class Drivers extends CI_Controller 
{ 

    function __construct() 
    { 
     parent::__construct(); 
     $this->load->model('driver_model'); 
     $this->load->helper('url', 'form', 'html', 'json'); 
    } 

    function index() 
    { 
     // Just loads the main Page of the Drivers Area 
     $data['metatitle'] = "Auto Ninja | Drivers Members Area | Locally Rated Garages &amp; Mechanics"; 
     $data['metadescription'] = "Garages &amp Mechanics"; 
     $data['metakeywords'] = "Car Repair, Car Service, Car MOT"; 
     $this->load->view('drivers/header_drivers.inc.php', $data); 
     $this->load->view('drivers/index'); 
     $this->load->view('drivers/footer_index.inc.php'); 
    } 


    public function driver_gettown() 
    { 
     if (isset($_GET['term'])){ 
      exit; 
     } 
     $this->load->model('driver_model'); 
     $q = ucfirst($_GET['term']); 
     $this->driver_model->driver_get_towns($q); 
    } 

} 

和意见/帮助将不胜感激。

function driver_addjob() 
    { 
     // Loads the Add New Job Form for the Website 
     $this->load->helper('form'); 
     $this->load->library(array('form_validation', 'session')); 
     $this->load->model('driver_model'); 
     $this ->form_validation->set_error_delimiters('<span class="error">', '</span>'); 
     // Validate the form fields 
     $this->form_validation->set_rules('town', 'Nearest Town or City', 'trim|required|xss_clean'); 
     // Populates dropdown "town" from the database ??? 

     if ($this->form_validation->run() == FALSE) 

     { 
      $data['metatitle'] = "Auto Ninja | Drivers - Add New Job | Locally Rated Garages &amp; Mechanics"; 
      $data['metadescription'] = "Garages &amp Mechanics"; 
      $data['metakeywords'] = "Car Repair, Car Service, Car MOT"; 
      $this->load->view('drivers/header_drivers.inc.php', $data); 
      $this->load->view('drivers/driver_addjob.php'); 
      $this->load->view('drivers/footer_index.inc.php'); 
     } 
     else 
     { 
      $townid = $this->input->post('town'); 
      $work_jobtitle = $this->input->post('jobtitle'); 
      $this->driver_model->driver_add_job ($townid); 
      $this->session->set_flashdata('message', 'your work request has been added to the system'); 
      $data['metatitle'] = "Auto Ninja | Drivers - Add New Jobs Success | Locally Rated Garages &amp; Mechanics"; 
      $data['metadescription'] = "Garages &amp Mechanics"; 
      $data['metakeywords'] = "Car Repair, Car Service, Car MOT"; 
      $this->load->view('drivers/header_drivers.inc.php', $data); 
      $this->load->view('drivers/driver_addjob_success'); 
      $this->load->view('drivers/footer_index.inc.php'); 
     } 

    } 
+0

你能展示Firebug的反应是什么样子吗? – 2013-03-06 15:42:21

+0

接受\t application/json,text/javascript,*/*; Q = 0.01 接受编码\t gzip的,放气 主机\t php.codeigniter.server 的Referer \t HTTP://php.codeigniter.server/drivers/driver_addjob 用户代理\t的Mozilla/5.0(Macintosh上;英特尔的Mac OS X 10.8; rv:19.0)Gecko/20100101 Firefox/19.0 X-Requested-With \t XMLHttpRequest 200 OK 50ms – Richard 2013-03-06 21:03:39

+0

我注意到的是页面的页眉和页脚似乎只能看着控制台窗口返回。 – Richard 2013-03-06 21:07:53

回答

0

好吧我终于想出了这一个。由于某种原因,响应URL出现在 http://php.codeigniter.server/drivers/drivers/driver_gettown?term=ed

因此控制器被添加两次。我更新了Java到

(function($){ 
    $("#town").autocomplete({ 
     source :"driver_gettown", 
     minLength : 3, 
     dataType:'JSON', 
     type:'POST' 
    }); 
})(jQuery); 

即不包括控制器,它的工作原理!所以有点混淆,它如何知道哪个控制器可以找到该方法,但是谁在乎它。这将是很高兴知道,因为它可能仍然会惹恼我...可能是JSON调用在URL maby中引用它?

相关问题