JQUERY-在表單文字欄位輸入開頭文字,自動顯示相關文字

如:http://game.acd.idv.tw的搜尋

請先下載:http://ok6.cc/~css/100920/jaueryautocomplete.zip

下面範例需要資料庫哦,所以要自行建立資料庫哦。

在<head>...</head>之間加上下面語法:

<link rel="stylesheet" type="text/css" href="jquery.autocomplete.css" />
<script type="text/javascript" src="jquery.js"></script> 
<script type="text/javascript" src="jquery.autocomplete.js"></script>
<script type="text/javascript"> 
$(function() {       
  $("#t1").autocomplete("s.php", {matchContains: true});      
});  
</script>

在<body>...</body>之間加上下面語法:

<form id="form1" name="form1" method="post" action="">
  <label for="t1"> </label>
  <input type="text" name="t1" id="t1" />
</form>

開一個新的php檔案,儲存成「s.php」,語法如下:

<?php  
header('Content-Type: text/html;charset=UTF-8');
require_once('Connections/conn.php');
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;   
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
$colname_Recordset1 = "%";
if (isset($_GET['q'])) {
 $colname_Recordset1 = $_GET['q']; 
}
//if (!$q) return; 
mysql_select_db($database_conn, $conn);
$query_Recordset1 = sprintf("SELECT gname FROM games WHERE gname LIKE %s LIMIT 0 , 10", GetSQLValueString("%" . $colname_Recordset1 . "%", "text"));
$Recordset1 = mysql_query($query_Recordset1, $conn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
do{
     printf("%s\n" ,$row_Recordset1['gname'] );
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
?> 

就可以用了。

arrow
arrow
    全站熱搜

    css6 發表在 痞客邦 留言(5) 人氣()