Heim  >  Artikel  >  Backend-Entwicklung  >  PHP+Ajax-Echtzeiteingabe, automatische Suchabgleichsmethode

PHP+Ajax-Echtzeiteingabe, automatische Suchabgleichsmethode

墨辰丷
墨辰丷Original
2018-06-07 09:57:212572Durchsuche

Dieser Artikel stellt hauptsächlich die Methode der automatischen Suche und des Abgleichs mithilfe der PHP+Ajax-Echtzeiteingabe vor. Ich hoffe, dass er für alle hilfreich ist.

dropbox_index.php

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>主搜索引擎下拉框自动显示数据</title>
</head>
<script src="js/dropbox.js" type="text/javascript"></script>
<link href="css/dropbox.css" type="text/css" rel="stylesheet"/>
<body>
<form action="" method="post" enctype="multipart/form-data">
        <input name="txt" id="txt" class="txt" type="text" onkeyup ="showHint(this.value)" />    
        <input type="submit" class="btn" name="submit" value="Upload" /><br />
        <span id="txtHint" class="file-box"></span>
</form>
</body>
</html>

dropbox.js ajax

var xmlHttp
    function showHint(str)
    {
      if (str.length==0)
      { 
       document.getElementById("txtHint").innerHTML=""
       return
      }
      xmlHttp=GetXmlHttpObject()
      if (xmlHttp==null)
       {
         alert ("Browser does not support HTTP Request")
         return
       } 
      var url="responsepage.php"
      url=url+"?q="+str
      url=url+"&sid="+Math.random()
      xmlHttp.onreadystatechange=stateChanged 
      xmlHttp.open("GET",url,true)
      xmlHttp.send(null)
    }      
    //设置回调函数
    function stateChanged() 
    { 
      if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
       { 
        document.getElementById("txtHint").innerHTML=xmlHttp.responseText 
       } 
    }
    function GetXmlHttpObject()
    {
      var xmlHttp=null;
      try
       {
         // Firefox, Opera 8.0+, Safari
         xmlHttp=new XMLHttpRequest();
      }
      catch (e)
      {
         // Internet Explorer
         try
         {
          xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
         }
         catch (e)
         {
          xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
         }
       }
      return xmlHttp;
    }

dropbox.css

.txt{ height:22px; border:1px solid #cdcdcd; width:220px;border-right:none;}
.btn{ background-color:#FFF; border:1px solid #CDCDCD;height :26px; width:70px;}

responsepage.php

<?php
  //get the q parameter from URL  
  $q=$_GET["q"];
  //全部小写化
  $q=strtolower($q);
  //非空验证
  if(isset($q) && $q != &#39;&#39;)
  {
    $con = mysql_connect("localhost","root","lifu");
    if(!$con)
    {  
      die(&#39;Could not connect: &#39; .mysql_error());
    }
    mysql_select_db("my_db",$con);
    
    $sql = "select * from persons";
    //$sql="SELECT FirstName FROM Persons where Firstname like &#39;%$q%&#39;";
  
    $result =mysql_query($sql,$con);
    while($row = mysql_fetch_array($result))
     {
       //匹配判断
       if(stristr(strtolower($row[&#39;FirstName&#39;]),$q))
       {
         //echo "-----------------Persons-----------------";
         echo /* "firstname:" .*/ $row[&#39;FirstName&#39;] . "<br />";
         //echo "lastname:" . $row[&#39;LastName&#39;] . "<br />";
         //echo "age:" . $row[&#39;Age&#39;] . "<br />";
       }
       //echo $row[&#39;FirstName&#39;] . "<br />";
     }
    mysql_close($con);
   }
?>

Datenbank:

Zusammenfassung : Das Obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, er wird für das Studium aller hilfreich sein.

Verwandte Empfehlungen:

Die Funktion der ZipArchive-Funktion in PHP

php So serialisieren Sie Daten und Deserialisierung

So erhalten Sie den Betriebssystemtyp in PHP

Das obige ist der detaillierte Inhalt vonPHP+Ajax-Echtzeiteingabe, automatische Suchabgleichsmethode. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn