📄 php_ajax_suggest.asp@output=print
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>PHP and AJAX Suggest</title>
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />
<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
<script src="clienthint.js" type="text/javascript"></script>
</head>
<body>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
<h1>PHP and AJAX Suggest</h1>
<a href="php_ajax_xmlhttprequest.asp"><img border="0" src="../images/btn_previous.gif" width="100" height="20" alt="Previous" /></a>
<a href="php_ajax_xml.asp"><img border="0" src="../images/btn_next.gif" width="100" height="20" alt="Next" /></a>
<hr />
<h2>AJAX Suggest</h2>
<p>In the AJAX example below we will demonstrate how a web page can communicate
with a web server online as a user enters data into a web form.</p>
<hr>
<h2>Type a Name in the Box Below</h2>
<form>
First Name: <input id="txt1" onkeyup="showHint(this.value)" type="text">
</form>
<p>Suggestions: <span id="txtHint"></span></p>
<p>This example consists of three pages:</p>
<ul>
<li>a simple HTML form</li>
<li>a JavaScript</li>
<li>a PHP page</li>
</ul>
<hr>
<h2>The HTML Form</h2>
<p>This is the HTML page. It contains a simple HTML form and a link to a
JavaScript:</p>
<table class="ex" id="table11" border="1" width="100%">
<tr>
<td>
<pre><html>
<head>
<script src="clienthint.js"></script>
</head></pre>
<pre><body></pre>
<pre><form>
First Name:
<input type="text" id="txt1"
onkeyup="showHint(this.value)">
</form></pre>
<pre><p>Suggestions: <span id="txtHint"></span></p> </pre>
<pre></body>
</html></pre>
</td>
</tr>
</table>
<h2>Example Explained - The HTML Form</h2>
<p>As you can see, the HTML page above contains a simple HTML form with an input field called
"txt1".</p>
<p>The form works like this:</p>
<ol>
<li>An event is triggered when the user presses, and releases a key in the
input field</li>
<li>When the event is triggered, a function called showHint() is executed.
</li>
<li>Below the form is a <span> called "txtHint". This is used as a placeholder for
the return data of the showHint() function.</li>
</ol>
<hr>
<h2>The JavaScript</h2>
<p>The JavaScript code is stored in "clienthint.js" and linked to the HTML
document:</p>
<table class="ex" id="table12" border="1" width="100%">
<tr>
<td>
<pre>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
}
}</pre>
<pre>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;
}</pre>
</td>
</tr>
</table>
<h2>Example Explained</h2>
<p><b>The showHint() Function</b></p>
<p>This function executes every time a character is entered in the input field.</p>
<p>If there is some input in the text field (str.length > 0) the function
executes the following:</p>
<ol>
<li>Defines the url (filename) to send to the server</li>
<li>Adds a parameter (q) to the url with the content of the input field
</li>
<li>Adds a random number to prevent the server from using a cached file</li>
<li>Calls on the GetXmlHttpObject function to create an XMLHTTP object, and tells the object to execute a function
called stateChanged when a change is triggered</li>
<li>Opens the XMLHTTP object with the given url.</li>
<li>Sends an HTTP request to the server</li>
</ol>
<p>If the input field is empty, the function simply clears the content of the
txtHint placeholder.</p>
<p><b>The stateChanged() Function</b></p>
<p>This function executes every time the state of the XMLHTTP
object changes.</p>
<p>When the state changes to 4 (or to "complete"), the content of the txtHint
placeholder is filled with the response text. </p>
<p><b>The GetXmlHttpObject() Function</b></p>
<p>AJAX applications can only run in web browsers with complete XML support.</p>
<p>The code above called a function called GetXmlHttpObject().</p>
<p>The purpose of the function is to solve the problem of creating different
XMLHTTP objects for different browsers.</p>
<p>This is explained in the previous chapter.</p>
<hr>
<h2>The PHP Page</h2>
<p>The server page called by the JavaScript code is a simple PHP file called "gethint.php".</p>
<p>The code in the "gethint.php" checks an array of names and returns the
corresponding names to the client:</p>
<table class="ex" id="table10" border="1" width="100%">
<tr>
<td>
<pre><?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";</pre>
<pre>//get the q parameter from URL
$q=$_GET["q"];</pre>
<pre>//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;
?></pre>
</td>
</tr>
</table>
<p>If there is any text sent from the JavaScript (strlen($q) > 0) the following
happens:</p>
<ol>
<li>Find a name matching the characters sent from the JavaScript</li>
<li>If more than one name is found, include all names in the response string</li>
<li>If no matching names were found, set response to "no suggestion"</li>
<li>If one or more matching names were found, set response to these names</li>
<li>The response is sent to the "txtHint" placeholder</li>
</ol>
<hr />
<a href="php_ajax_xmlhttprequest.asp"><img border="0" src="../images/btn_previous.gif" width="100" height="20" alt="Previous" /></a>
<a href="php_ajax_xml.asp"><img border="0" src="../images/btn_next.gif" width="100" height="20" alt="Next" /></a>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -