使用php(codeigniter)和数据库的JQuery ui自动完成(JQuery ui autocomplete with php (codeigniter) and database)

编程入门 行业动态 更新时间:2024-10-23 08:37:56
使用php(codeigniter)和数据库的JQuery ui自动完成(JQuery ui autocomplete with php (codeigniter) and database)

我有这个ac.php文件和这个result.php文件我想在ac.php文件中使用JQuery UI中的自动完成,但我希望它从result.php中检索数据作为源。 我只是不知道该怎么做。 我试过这种方式,但它不起作用

ac.php

<html> <head> <link type="text/css" href="jqui/css/ui-lightness/jquery-ui-1.8.11.custom.css" rel="stylesheet" /> <script type="text/javascript" src="jqui/js/jquery-1.5.1.min.js"></script> <script type="text/javascript" src="jqui/js/jquery-ui-1.8.11.custom.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#tags").autocomplete("result.php"); }); </script> </head> <body> <div class="ui-widget"> <label for="tags">Tags: </label> <input id="tags"> </div> </body> </html>

这是result.php

<?php $arrResults = array('option 1', 'option 2', 'option 3'); // Print them out, one per line echo implode("\n", $arrResults); ?>

I have this ac.php file and this result.php file I want to use the autocomplete in JQuery UI in the ac.php file, but I want it to retrieve the data from result.php as a source. I just dont know how to do it. I tried this way, but its not working

ac.php

<html> <head> <link type="text/css" href="jqui/css/ui-lightness/jquery-ui-1.8.11.custom.css" rel="stylesheet" /> <script type="text/javascript" src="jqui/js/jquery-1.5.1.min.js"></script> <script type="text/javascript" src="jqui/js/jquery-ui-1.8.11.custom.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#tags").autocomplete("result.php"); }); </script> </head> <body> <div class="ui-widget"> <label for="tags">Tags: </label> <input id="tags"> </div> </body> </html>

this is result.php

<?php $arrResults = array('option 1', 'option 2', 'option 3'); // Print them out, one per line echo implode("\n", $arrResults); ?>

最满意答案

引用自动完成的文档 :

自动完成插件期望该字符串指向将返回JSON数据的URL资源。

因此,您应该使用以下内容代替您的代码:

<?php $arrResults = array('option 1', 'option 2', 'option 3'); echo json_encode($arrResults); ?>

json_encode()函数将返回与您的数组对应的JSON字符串。

Quoting the documentation of Autocomplete :

the Autocomplete plugin expects that string to point to a URL resource that will return JSON data.

So, instead of your code, you should use something like this :

<?php $arrResults = array('option 1', 'option 2', 'option 3'); echo json_encode($arrResults); ?>

The json_encode() function will return the JSON string that corresponds to your array.

更多推荐

本文发布于:2023-08-07 20:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465597.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自动完成   数据库   php   codeigniter   JQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!