using JQuery autocomplete from database with PHP (CodeIgniter)

亡梦爱人 提交于 2019-12-23 03:44:11

问题


So I want to use this plugin http://docs.jquery.com/Plugins/autocomplete

I want to retrieve all the user names from database using codeigniter then store them in a var in javascript (If this is a good way) then use it in autocomplete. Also, I want the user if he/she enters any other text it wont be accepted, it has to be already stored in database only.

Thanx in advanced :)


回答1:


OK, Here is how I would structure it:

First, you have to create a file to serve your data from your backend database. According to the jQuery Autocomplete Docs, your backend will need to return a list of options one per line.

Let's call our php file, get_results.php:

<?php

// Do your DB calls here to fill an array of results
$arrResults = array('option 1', 'option 2', 'option 3');

// Print them out, one per line
echo implode("\n", $arrResults); 

Then, in your JavaScript code, you'd do something like this:

$("#myTextBox").autocomplete('get_results.php');

That is the very basic of how I would do it. Hopefully, you can go from there. Here are some important resources:

  • PHP Basics
  • PHP Database Connections
  • jQuery Autocomplete Docs


来源:https://stackoverflow.com/questions/5487872/using-jquery-autocomplete-from-database-with-php-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!