Kay
Kay

Reputation: 213

using JQuery autocomplete from database with PHP (CodeIgniter)

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 :)

Upvotes: 1

Views: 8124

Answers (1)

Jesse Bunch
Jesse Bunch

Reputation: 6679

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:

Upvotes: 4

Related Questions