⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 php_ajax_suggest.asp@output=print

📁 W3Schools tutorial..web designing
💻 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>&lt;html&gt;
&lt;head&gt;
&lt;script src=&quot;clienthint.js&quot;&gt;&lt;/script&gt; 
&lt;/head&gt;</pre>
		<pre>&lt;body&gt;</pre>
		<pre>&lt;form&gt; 
First Name:
&lt;input type=&quot;text&quot; id=&quot;txt1&quot;
onkeyup=&quot;showHint(this.value)&quot;&gt;
&lt;/form&gt;</pre>
		<pre>&lt;p&gt;Suggestions: &lt;span id=&quot;txtHint&quot;&gt;&lt;/span&gt;&lt;/p&gt; </pre>
		<pre>&lt;/body&gt;
&lt;/html&gt;</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 
&quot;txt1&quot;.</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 &lt;span&gt; called &quot;txtHint&quot;. 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 &quot;clienthint.js&quot; 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(&quot;txtHint&quot;).innerHTML=&quot;&quot;
  return
  }
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
  {
  alert (&quot;Browser does not support HTTP Request&quot;)
  return
  } 
var url=&quot;gethint.php&quot;
url=url+&quot;?q=&quot;+str
url=url+&quot;&amp;sid=&quot;+Math.random()
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open(&quot;GET&quot;,url,true)
xmlHttp.send(null)
} 

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState==&quot;complete&quot;)
 { 
 document.getElementById(&quot;txtHint&quot;).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(&quot;Msxml2.XMLHTTP&quot;);
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);
  }
 }
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 &gt; 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 &quot;complete&quot;), the content of the txtHint 
placeholder is filled with the response text.&nbsp; </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 &quot;gethint.php&quot;.</p>
<p>The code in the &quot;gethint.php&quot; 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>&lt;?php
// Fill up array with names
$a[]=&quot;Anna&quot;;
$a[]=&quot;Brittany&quot;;
$a[]=&quot;Cinderella&quot;;
$a[]=&quot;Diana&quot;;
$a[]=&quot;Eva&quot;;
$a[]=&quot;Fiona&quot;;
$a[]=&quot;Gunda&quot;;
$a[]=&quot;Hege&quot;;
$a[]=&quot;Inga&quot;;
$a[]=&quot;Johanna&quot;;
$a[]=&quot;Kitty&quot;;
$a[]=&quot;Linda&quot;;
$a[]=&quot;Nina&quot;;
$a[]=&quot;Ophelia&quot;;
$a[]=&quot;Petunia&quot;;
$a[]=&quot;Amanda&quot;;
$a[]=&quot;Raquel&quot;;
$a[]=&quot;Cindy&quot;;
$a[]=&quot;Doris&quot;;
$a[]=&quot;Eve&quot;;
$a[]=&quot;Evita&quot;;
$a[]=&quot;Sunniva&quot;;
$a[]=&quot;Tove&quot;;
$a[]=&quot;Unni&quot;;
$a[]=&quot;Violet&quot;;
$a[]=&quot;Liza&quot;;
$a[]=&quot;Elizabeth&quot;;
$a[]=&quot;Ellen&quot;;
$a[]=&quot;Wenche&quot;;
$a[]=&quot;Vicky&quot;;</pre>
		<pre>//get the q parameter from URL
$q=$_GET[&quot;q&quot;];</pre>
		<pre>//lookup all hints from array if length of q&gt;0
if (strlen($q) &gt; 0)
{
$hint=&quot;&quot;;
for($i=0; $i&lt;count($a); $i++)
  {
  if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
    {
    if ($hint==&quot;&quot;)
      {
      $hint=$a[$i];
      }
    else
      {
      $hint=$hint.&quot; , &quot;.$a[$i];
      }
    }
  }
}

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

//output the response
echo $response;
?&gt;</pre>
		</td>
	</tr>
</table>
<p>If there is any text sent from the JavaScript (strlen($q) &gt; 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 &quot;no suggestion&quot;</li>
	<li>If one or more matching names were found, set response to these names</li>
	<li>The response is sent to the &quot;txtHint&quot; 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 + -