Home > Backend Development > PHP Tutorial > PHP and AJAX Suggest_PHP Tutorial

PHP and AJAX Suggest_PHP Tutorial

WBOY
Release: 2016-07-13 17:07:00
Original
1378 people have browsed it

PHP and AJAX Suggest

This suggestion is imitated by Google. Google is always at the forefront of the world’s technology. Let’s take a look at some examples.

Look at the html file first.

<form> 
First Name:
<input type="text" id="txt1"
onkeyup="showHint(this.value)">
</form>
Copy after login
<p>Suggestions: <span id="txtHint"></span></p>
Copy after login
下面用js文件的ajax文件来处理数据.
Copy after login
<pre class="brush:php;toolbar:false">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="gethint.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;
 } 
}
Copy after login
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;
}
Copy after login
 
Copy after login
这个文件只是返回php处理的数据,而我们的php文件如下.
Copy after login
<pre class="brush:php;toolbar:false"><?php
// Fill up array with names
$a[]="Anna";
$a[]="Brittany";
$a[]="Cinderella";
$a[]="Diana";
$a[]="Eva";
$a[]="Fiona";
$a[]="Gunda";
$a[]="Hege";
$a[]="Inga";
$a[]="Johanna";
$a[]="Kitty";
$a[]="Linda";
$a[]="Nina";
$a[]="Ophelia";
$a[]="Petunia";
$a[]="Amanda";
$a[]="Raquel";
$a[]="Cindy";
$a[]="Doris";
$a[]="Eve";
$a[]="Evita";
$a[]="Sunniva";
$a[]="Tove";
$a[]="Unni";
$a[]="Violet";
$a[]="Liza";
$a[]="Elizabeth";
$a[]="Ellen";
$a[]="Wenche";
$a[]="Vicky";
Copy after login
//get the q parameter from URL
$q=$_GET["q"];
Copy after login
//lookup all hints from array if length of q>0
if (strlen($q) > 0)
{
$hint="";
for($i=0; $i<count($a); $i++)
  {
  if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
    {
    if ($hint=="")
      {
      $hint=$a[$i];
      }
    else
      {
      $hint=$hint." , ".$a[$i];
      }
    }
  }
}

//Set output to "no suggestion" if no hint were found
//or to the correct values
if ($hint == "")
{
$response="no suggestion";
}
else
{
$response=$hint;
}

//output the response
echo $response;
?>
Copy after login
好了我们的确suggest效果就完成了.
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/630490.htmlTechArticlePHP and AJAX Suggest This suggestion imitates the effect of Google. Google is always at the forefront of the world's technology. , let’s take a look at an example. Let’s look at the html file first. form First Name:inp...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template