我已经从jquery做了自动完成功能。我已经测试过了,除了打开页面太长时加载,它工作正常。所以我想再次创建一个文件,这个页面可以更快地打开或重新加载。
这是自动填充文件
<?php
include "../config/config.php";
$region = $_GET['region'];
?>
<script>
$(function() {
var availableTags = [
<?php
$sql=mysql_query("SELECT country_name FROM country WHERE region='$region' ORDER BY country_name");
while($f=mysql_fetch_array($sql))
{
echo " '".$f['country_name']."', ";
}
?>
];
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#tags" )
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).autocomplete( "instance" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
minLength: 0,
source: function( request, response ) {
// delegate back to autocomplete, but extract the last term
response( $.ui.autocomplete.filter(
availableTags, extractLast( request.term ) ) );
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
});
</script>
</head>
<body>
<div class="ui-widget">
<label >Type country: </label>
<input id="tags" size="50">
</div>
如何使用ajax和sql将此文件分成两个文件?
答案 0 :(得分:0)
将PHP放在一个单独的文件中,例如名为“getRegions.php”
<?php
include "../config/config.php";
$region = $_GET['term']; //this has to be changed to "term" !!!!
$sql=mysql_query("SELECT country_name FROM country WHERE region='$region' ORDER BY country_name");
$regions = array();
while($f=mysql_fetch_array($sql))
{
$regions[] = $f['country_name'];
}
echo json_encode($regions);
然后,不要在您的javascript部分中使用var availableTags
,而是将.ajax()
source属性更改为source: 'getRegions.php'
。
这应该可以做到!