Codeigniter 2从数据库自动完成

时间:2013-03-06 12:21:07

标签: jquery-ui codeigniter-2

我正在尝试在表单上设置一个自动完成输入框,虽然一切似乎都有效但我没有将数据传递给输入框。 Firebug报告成功,但没有。我想知道是否有人可以查看我的代码,看看是否有可能导致它的明显错误。

脚本是:

(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');
        }

    }

1 个答案:

答案 0 :(得分: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中引发它吗?