(自訂功能) 自動完成與提示: 表單欄位 共用功能 (Ajax -->  PHP (JSON/XML)
 
1. Javascript 程式碼片斷:
 
////////////  BEGIN : AutoSuggest 自動完成 For TIPTOP Ajax Function /////////////////
// AutoSuggest --> pnu_file.pnu031 輸入關鍵字 系統自動完成與提示可用字串
var options_pnu031 = {
script: function (input) { return "http://127.0.0.1/glmc/ajax/ajax_autosuggest.php?input="+input; },
varname:"input"
};
 var as_pnu031 = new bsn.AutoSuggest('pnu031', options_pnu031);
 ////////////  END : AutoSuggest 自動完成 For TIPTOP Ajax Function //////////////////////////
 
1. PHP 程式碼片斷:
 
////////////  BEGIN : AutoSuggest 自動完成 For TIPTOP Ajax Function /////////////////
<?php
$input =  $_GET['input'];
$len = strlen($input);
$limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 6;
$link  = OCILogon("ds", "ds", "topprod"); 
$query = "select distinct afa01,afa02
  from ( 
  select afa01,afa02
  from db1.afa_file
                  where afaacti='Y'
  union all 
  select afa01,afa02
  from db2.afa_file
                  where afaacti='Y'
  union all 
  select afa01,afa02
  from db3.afa_file
                  where afaacti='Y' ) order by 1";
//echo "$query";
$s = OCIParse($link,$query);
            if(! $s)
            {
                $e = ocierror();
                var_dump($e);
            }
            ociexecute($s);
    $Rrows = ocifetchstatement($s, $results);
    $aUsers = array ();
    $aInfo = array ();
    for ($i = 0; $i < $Rrows; $i++) 
    {
    $aUsers[$i]= $results["AFA01"][$i];
    $aInfo[$i] = $results["AFA02"][$i];
    }
            //This will print all the elements in the array
            //or you can print one element=$myarray[0]
  #  for ($j=0; $j <  $Rrows; $j++)
  #  {
      #      echo "$j:" . $aUsers[$j]."<br>\n";
  #  } 
$aResults = array();
$count = 0;
if ($len)
{
for ($i=0;$i<count($aUsers);$i++)
{
// had to use utf_decode, here
// not necessary if the results are coming from oracle
//
if (substr($aUsers[$i],0,$len) == $input)
{
$count++;
$aResults[] = array( "id"=>($i+1) ,"value"=>htmlspecialchars($aUsers[$i]), "info"=>htmlspecialchars($aInfo[$i]) );
}
if ($limit && $count==$limit)
break;
}
}
header ("Expires: Mon, 26 Jul 2000 05:00:00 GMT"); // Date in the past
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header ("Pragma: no-cache"); // HTTP/1.0
if (isset($_REQUEST['json']))
{
header("Content-Type: application/json; charset=utf-8");
echo "{\"results\": [";
$arr = array();
for ($i=0;$i<count($aResults);$i++)
{
$arr[] = "{\"id\": \"".$aResults[$i]['id']."\", \"value\": \"".$aResults[$i]['value']."\", \"info\": \"\"}";
}
echo implode(", ", $arr);
echo "]}";
}
else
  {
header("Content-Type: text/xml; charset=utf-8");
echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?><results>";
for ($i=0;$i<count($aResults);$i++)
{
echo "<rs id=\"".$aResults[$i]['id']."\" info=\"".$aResults[$i]['info']."\">".$aResults[$i]['value']."</rs>";
}
echo "</results>";
       }
    OCIFreeStatement($s);
   OCILogoff($link); 
?>
 

 

 

arrow
arrow
    全站熱搜

    jeffreyhu 發表在 痞客邦 留言(0) 人氣()