国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > AJAX > 正文

如何使用 jQuery(Ajax)/PHP/MySQL實(shí)現(xiàn)自動完成功能

2024-09-01 08:26:22
字體:
供稿:網(wǎng)友

使用 jQuery(Ajax)/PHP/MySQL實(shí)現(xiàn)自動完成功能

JavaScript代碼

 

<script src="jquery-1.2.1.pack.js" type="text/javascript"></script>
<script type="text/javascript">

function lookup(inputString) {
if(inputString.length == 0) {
// Hide the suggestion box.
(‘#suggestions’).hide();
} else {
.post("rpc.php", {queryString: ""+inputString+""}, function(data){
if(data.length >0) {
(‘#suggestions’).show();
(‘#autoSuggestionsList’).html(data);
}
});
}
} // lookup

function fill(thisValue) {
(‘#inputString’).val(thisValue);
(‘#suggestions’).hide();
}

</script>

 

JS的解釋:

好,從上面的代碼看到,我們需要連接到一個叫做rpc.php的文件,這個文件處理所有的操作。

lookup函數(shù)使用從文本輸入框中得到的單詞然后使用jQuery中Ajax的方法POST把它傳給rpc.php。

如果輸入字符 ‘inputString’是‘0’(Zero,譯注:在這里是指在搜索框中沒輸入任何內(nèi)容),建議框就被隱藏,這也很人性化,你想,如果在搜索框中沒有輸入任何東西,你也不期望會出現(xiàn)個建議提示框。

如果輸入框中有內(nèi)容,我們就得到了這個 ‘inputString’并傳遞給rpc.php頁面,然后jQuery 的.post()函數(shù)被使用,如下:

.post(url, [data], [callback])

‘callback’部分可以關(guān)聯(lián)一個函數(shù),這個比較有意思,只有在數(shù)據(jù)(data)被加載成功的時候才會執(zhí)行(譯注:此處為意譯,沒看懂原文:<).

如果返回的數(shù)據(jù)(data)不為空(也就是說,有東西要顯示),那就顯示搜索提示框并且使用返回的數(shù)據(jù)(data)來代替其中的html代碼。

就這么簡單!

PHP后臺程序(rpc.php):

如你所知(譯注:不好意思,看王小波就學(xué)會了這么個口頭禪),我的php后臺程序都叫做rpc.php(RPC指遠(yuǎn)程過程調(diào)用),而沒用它實(shí)際執(zhí)行的功能來命名,但是也還不錯了。

 

// PHP5 Implementation - uses MySQLi.
db = new mysqli(‘localhost’, ‘root’ ,”, ‘autoComplete’);
if(!db) {
// Show error if we cannot connect.
echo ‘ERROR: Could not connect to the database.’;
} else {
// Is there a posted query string?
if(isset(_POST[‘queryString’])) {
queryString = _POST[‘queryString’];
// Is the string length greater than 0?
if(strlen(queryString) >0) {
// Run the query: We use LIKE ‘queryString%’
// The percentage sign is a wild-card, in my example of countries it works like this…
// queryString = ‘Uni’;
// Returned data = ‘United States, United Kindom’;
query = db->query("SELECT value FROM countries WHERE value LIKE ‘queryString%’ LIMIT 10");
if(query) {
// While there are results loop through them - fetching an Object (i like PHP5 btw!).
while (result = query ->fetch_object()) {
// Format the results, im using <li> for the list, you can change it.
// The onClick function fills the textbox with the result.
echo ‘<li onclick="fill(’‘.result->value.’‘);">’.result->value.‘</li>’;
}
} else {
echo ‘ERROR: There was a problem with the query.’;
}
} else {
// Dont do anything.
} // There is a queryString.
} else {
echo ‘There should be no direct access to this script!’;
}
}

?>

 

PHP代碼解釋

鑒于代碼中我已經(jīng)加了很多注釋,在這里我就不再說的很詳細(xì)了。

一般情況下,需要接收這個 ‘QueryString’ 然后在其最后使用通配符產(chǎn)生一個查詢語句。

這意味著在這種情況下,每次敲進(jìn)去一個字符都需要產(chǎn)生一個查詢語句,如果一直都這樣做的話,恐怕MYSQL會受不了。但是為了盡量的簡化這個過程,這種做法對一個規(guī)模較小的應(yīng)用應(yīng)該沒什么問題。

這段php代碼你需要在自己的系統(tǒng)中稍作修改,比如你需要更新‘query’到你自己的數(shù)據(jù)庫,需要看在哪里放你數(shù)據(jù)庫表的列名等等。

CSS樣式

我使用的是CSS3,天哪,它真的很好用,雖然在Firefox 或者Safari瀏覽器上會有功能限制。

 

<style type="text/css">
.suggestionsBox {
position: relative;
left: 30px;
margin: 10px 0px 0px 0px;
width: 200px;
background-color: #212427;
-moz-border-radius: 7px;
-webkit-border-radius: 7px;
border: 2px solid #000;
color: #fff;
}

.suggestionList {
margin: 0px;
padding: 0px;
}

.suggestionList li {
margin: 0px 0px 3px 0px;
padding: 3px;
cursor: pointer;
}

.suggestionList li:hover {
background-color: #659CD8;
}
</style>

 

CSS代碼都很標(biāo)準(zhǔn),沒什么需要特別指出的。

主文件HTML

 

<div>
<div>

Type your county (for the demo):
<input size="30" id="inputString" onkeyup="lookup(this.value);" type="text" />

</div> <div class="suggestionsBox" id="suggestions" style="display: none;">

<img src="upArrow.png" style="position: relative; top: -12px; left: 30px" alt="upArrow" />

<div class="suggestionList" id="autoSuggestionsList">

</div>

</div>

</div>

 

這是主文件的部分html代碼,你需要添加的就是一個輸入框,并且把 ‘onkeyup’ 函數(shù)設(shè)置為lookup(this.value)。另外,我建議你不要修改它的ID,如果你不想修改上面的Javascript代碼的話。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 湘乡市| 襄城县| 延安市| 克山县| 廊坊市| 峨眉山市| 涟水县| 铜川市| 乌兰浩特市| 白河县| 佛学| 手游| 陕西省| 乐陵市| 视频| 正安县| 通山县| 黔南| 冕宁县| 安仁县| 洛隆县| 理塘县| 徐汇区| 静安区| 宁国市| 乐业县| 凤翔县| 卓尼县| 乃东县| 绵阳市| 竹溪县| 三明市| 三河市| 房产| 宜州市| 梨树县| 绵竹市| 石林| 砚山县| 湄潭县| 濮阳县|