[Webinar] Streamline your web hosting managementRegister Today


Autosuggest box not working correctly using json and php?

Posted on 2010-04-10
Medium Priority
Last Modified: 2013-12-13

I am using the following autosuggest code:

Have been able to successfully tap into my database using the JSON method, however, when I type something into the autosuggest field, the auto suggestions do not change with the different key presses.

For example: When I type in any letter to start the auto-suggest, I always get the first 4 results in my database.  No matter what I type, I always get the same results.  If my letter matches a letter in the results, it changes color as expected. If I make another key press, the auto suggest box does not pair down the results, it just continues to show my first 4 db entries, and colors matching letters.

Can anyone help?  This is my code so far:


<form method="get" action="" class="asholder">
	<small style="float:right">Hidden ID Field: <input type="text" id="testid" value="" style="font-size: 10px; width: 20px;" disabled="disabled" /></small>
	<label for="testinput">Person</label>
	<input style="width: 200px" type="text" id="testinput" value="" /> 
	<input type="submit" value="submit" />
<script type="text/javascript">
	var options = {
		callback: function (obj) { document.getElementById('testid').value = obj.id; }
	var as_json = new bsn.AutoSuggest('testinput', options);




header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
	header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
	header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
	header ("Pragma: no-cache"); // HTTP/1.0
	if (isset($_REQUEST['json']))
		header("Content-Type: application/json");
			$sql="SELECT * FROM members";
			$data = mysql_query($sql);
			echo "{\"results\": [";
			while($results = mysql_fetch_array($data)){
			echo "{\"id\": \"".$results['id']."\", \"value\": \"".$results['LastName'].", ".$results['FirstName']."\", \"info\": \"".$results['NUID']."\"},";
			echo'{ id: "", value: "", info: "" }';
		echo "]}";


the bsn.AutoSuggest_2.1.3.js file has not been modified.

Open in new window

Question by:evibesmusic
  • 2

Author Comment

ID: 30368867

I found out that if I limit the number of query results the function works properly but, I don't want to limit the number of query results, I want to be able to select from all entries in the db.
LVL 82

Expert Comment

ID: 30426400
then try putting a large number in the options
LVL 12

Assisted Solution

by:Richard Davis
Richard Davis earned 400 total points
ID: 30447178
Looking at your SELECT statement, I noticed that you are never actually creating a WHERE clause by which to filter the select on. I do something similar on one of my custom admin systems I developed, only not with AJAX, but same principle.

I would modify your code so that the ajax call is sending the contents of the input box as each character is typed in and then in the dynamic_content.php, I would scrub the value passed in, then create a WHERE clause in the SELECT that uses a LIKE condition with %$value% as the like qualifier.

This will reduce the query's dataset to a much smaller number of returned records while also giving you back the data that specifically applies to the search criteria that you're wanting to have returned.

Hope this helped.

LVL 82

Accepted Solution

hielo earned 1600 total points
ID: 30458439
if (isset($_REQUEST['json']))

if (isset($_REQUEST['input']))

AND also:
 $sql="SELECT * FROM members";

 $sql="SELECT * FROM members WHERE `lastName` LIKE '" . mysql_real_escape_string($_REQUEST['input']) . "%' ";


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question