Question : jQuery Autocomplete with PHP / MySQL [PART 1]

( I am splitting my questions up in parts so that you can get 1000’s of points versus just 500.  Each question will build upon the previous one so make sure you look for [PART 2], [PART 3] and [PART 4] after you figure this one out. )

[PART 1] – Basic jQuery Autocomplete

I do not understand how the remote script accesses the data I enter into the text field. As always, the jquery ui documentation is leaving me confused. It says...

"When a String is used, the Autocomplete plugin expects that string to point to a URL resource that will return JSON data… The request parameter "term" gets added to that URL. "


To me, this means that I should configure my PHP script to access the value it was passed from $_GET[‘term’], however, in my hours of painful research trying to figure this out, I have seen $GET[‘q’] show up everywhere but haven’t found any documentation that explains it.

 Anyway, I am getting good json data back from the script, but I’m not passing the data to it properly.  Below is what I have so far.  If you could shed some light, I would be grateful.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
// This is my test page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<link href="includes18/jQuery/css/plt-theme/jquery-ui-1.8.2.custom.css" rel="stylesheet" type="text/css" />

<script src="includes18/jQuery/js/jquery-1.4.2.min.js" type="text/javascript"></script>

<script src="includes18/jQuery/js/jquery-ui-1.8.2.custom.min.js" type="text/javascript"></script>

    
    <script type="text/javascript">
	$(function() {
		$("#send_to").autocomplete({
			source: "find_ships.php",
			minLength: 2,
			select: function(event, ui) {
			}
		});
	});
	</script>
    </head>

<body>
	<p>Autocomplete Example</p>
    <div class="ui-widget">
        <label for="send_to">To: </label>
        <input id="send_to" size="60" />
	</div>
</body>
</html>


//This is my PHP script
<?php 

require_once('../Connections/BBA.php'); 

$term = $_GET['q'];

mysql_select_db($database_BBA, $BBA);
$query = "SELECT username FROM users WHERE username LIKE '$term%'";
$query_result = mysql_query($query, $BBA) or die(mysql_error());

$results = array();

//This creates a json array
while($row = mysql_fetch_assoc($query_result)) { 
		array_push($results, $row['username']);	
}
print json_encode($results);	

?>

Answer : jQuery Autocomplete with PHP / MySQL [PART 1]

>What's the deal with $_GET['q']?

In your case with Jquery UI Autocomplete it should be : $_GET['term']?
there's multiple autocomplete pugin, be careful
1:
$term = $_GET['term'];
Random Solutions  
 
programming4us programming4us