.ui-autocomplete-loading {
background: white url("images/ui-anim_basic_16x16.gif") right center no-repeat;
}
- #city { width: 25em; }
</style>
<script src="../../external/requirejs/require.js"></script>
<script src="../bootstrap.js">
$( "#log" ).scrollTop( 0 );
}
- $( "#city" ).autocomplete({
+ $( "#birds" ).autocomplete({
source: function( request, response ) {
$.ajax( {
- url: "http://gd.geobytes.com/AutoCompleteCity",
+ url: "search.php",
dataType: "jsonp",
data: {
- q: request.term
+ term: request.term
},
success: function( data ) {
-
- // Handle 'no match' indicated by [ "" ] response
- response( data.length === 1 && data[ 0 ].length === 0 ? [] : data );
+ response( data );
}
} );
},
- minLength: 3,
+ minLength: 2,
select: function( event, ui ) {
- log( "Selected: " + ui.item.label );
+ log( "Selected: " + ui.item.value + " aka " + ui.item.id );
}
} );
</script>
<body>
<div class="ui-widget">
- <label for="city">Your city: </label>
- <input id="city" type="text">
- Powered by <a href="http://geobytes.com">geobytes.com</a>
+ <label for="birds">Birds: </label>
+ <input id="birds">
</div>
<div class="ui-widget" style="margin-top:2em; font-family:Arial">
</div>
<div class="demo-description">
-<p>The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions are cities, displayed when at least three characters are entered into the field. The datasource is the <a href="http://geobytes.com">geobytes.com webservice</a>. That data is also available in callbacks, as illustrated by the Result area below the input.</p>
+<p>The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions are bird names, displayed when at least two characters are entered into the field.</p>
+<p>The datasource is a server-side script which returns JSONP data, specified via a function which uses <code>jQuery.ajax()</code> for the <code>source</code> option.</p>
</div>
</body>
</html>
<?php
-sleep( 3 );
+sleep( 2 );
// no term passed - just exit early with no response
if (empty($_GET['term'])) exit ;
$q = strtolower($_GET["term"]);
"Heuglin's Gull"=>"Larus heuglini"
);
-
$result = array();
foreach ($items as $key=>$value) {
if (strpos(strtolower($key), $q) !== false) {
}
// json_encode is available in PHP 5.2 and above, or you can install a PECL module in earlier versions
-echo json_encode($result);
+$output = json_encode($result);
+
+if ($_GET["callback"]) {
+ $output = $_GET["callback"] . "($output);";
+}
+
+echo $output;
-?>
\ No newline at end of file
+?>