如何给source:从Codeigniter获取jQuery自动完成的路径

时间:2013-07-30 15:44:38

标签: php jquery sql codeigniter

我在我的Codeigniter应用程序上使用了jQuery auto complete。但由于源路径被破坏,它无法正常工作。

这是我的代码:

  jQuery(document).ready(function(){
            $('.zipsearch').autocomplete({
                source:'jQueryAutocompleteRelatedFields.php', 
                minLength:2,
                select:function(evt, ui)
                {
                    // when a zipcode is selected, populate related fields in this form
                    this.form.city.value = ui.item.city;
                    this.form.state.value = ui.item.state;
                }
            });
        });

目前文件jQueryAutocompleteRelatedFields.php位于以下文件路径中:

  

WWW \ CI \应用\视图\搜索\ jQueryAutocompleteRelatedFields.php

     

本地主机\ CI \的index.php \搜索\ searchItem

是我的浏览器路径

我如何给出来源?


SearchITem是搜索控制器下的功能,

更新:jQueryAutocompleteRelatedFields.php

     <?php

class DB
{
    const DATABASE = 'inventory';
    const HOST = '127.0.0.1';
    const USERNAME = 'root';
    const PASSWORD = '';

    static private $pdo;

    static public function singleton()
    {
        if (!is_object(self::$pdo))
        {
            self::$pdo = new PDO('mysql:dbname=' . self::DATABASE . ';host=' . self::HOST, 
                                    self::USERNAME, 
                                    self::PASSWORD);
        }
        return self::$pdo;
    }

    private function __construct()
    {

    }

    public function __clone()
    {
        throw new Exception('You may not clone the DB instance');
    }
}

if (!isset($_REQUEST['term']))
{
    die('([])');
}

$st = DB::singleton()
        ->prepare(
            'select product_id, product_code, product_name ' .
            'from tbl_product ' .
            'where product_id like :product_id ' .
            'order by product_id asc ' .
            'limit 0,10');

$searchZip = $_REQUEST['term'] . '%';
$st->bindParam(':product_id', $searchZip, PDO::PARAM_STR);

$data = array();
if ($st->execute())
{
    while ($row = $st->fetch(PDO::FETCH_OBJ))
    {
        $data[] = array(
            'value' => $row->product_id ,
            'city' => $row->product_code ,
            'state' => $row->product_name
        );
    }
}
echo json_encode($data);
flush(); 

1 个答案:

答案 0 :(得分:0)

您应该使用带有正斜杠的浏览器路径。

source: 'localhost/CI/index.php/search/searchitem',

将问题转移到真实的Web服务器上会有问题,因为您需要使用相对路径。我强烈建议您将本地环境设置为尽可能与Web服务器相似。

编辑:

在CodeIgniter中,将以下代码添加到Search控制器:

public function fetchList(){

    // I assume your view is outputting an array ready to be encoded with JSON?
    return json_encode($this->load->view('search/jQueryAutocompleteRelatedFields.php', array(), true));

    // Try this if the code above doesn't work
    return json_encode(array('item1','item2','item3','item4'));

}

现在指向这样的AJAX源:

source: 'localhost/CI/index.php/search/fetchList',