在这个php示例中,每当文本框的内容改变时,都将触发一个事件,即用异步方式读取数据库,返回数组,显示结果。
<?php
// For example,
$conn=mysql_connect ("localhost", "MYSQL用户名", "密码");
mysql_select_db("表");
// Suggest Framework passes two variables:
//
// $type - the name of the input tag
// $q - the search string
if($_GET["type"] && $_GET["q"])
{
// This is an example of a three column response being sent back, with a header
$query = "SELECT word, " .
" chinese " .
"FROM d " .
"WHERE word LIKE '" . $_GET["q"] . "%' " .
"LIMIT 10";
$results = mysql_query($query);
if(mysql_num_rows($results))
{
// Header
$values[] = 'new Array("[词条]", "[中文含义]")';
while($result = mysql_fetch_object($results))
$values[] = 'new Array("' . $result->word . '", "' . $result->chinese . '")';
print "new Array(" . implode(", ", $values) . ");";
}
}
?>
上面的代码是,连接一个MYSQL数据库,然后在一个表的中读取数据。而你在html页面中只要稍稍做一下处理,看下面前台代码的文本框参数。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Suggest Framework example</title>
<style type="text/css">@import url("SuggestFramework.css");</style>
<style type="text/css">input { width: 400px; border: 1px solid #CCCCCC; }</style>
<script type="text/javascript" src="SuggestFramework.js"></script>
<script type="text/javascript">window.onload = initializeSuggestFramework;</script>
</head>
<body>
<input id="example2" name="example2" type="text" heading="true" action="backend.php" columns="2" />
</body>
</html>
[ 本帖最后由 卢巧音中文网 于 2006-8-30 10:57 编辑 ]