Successful Ajax instances

Source: Internet
Author: User

Successful Ajax instances

Demo1.php

 

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> untitled document </title>
<Script language = "JavaScript">
VaR XMLHTTP = NULL;
Function getxmlhttpobject ()
{
Try {
XMLHTTP = new XMLHttpRequest ();
} Catch (e ){
Try {
XMLHTTP = new activexobject ("msxml2.xmlhttp ");
} Catch (e ){
XMLHTTP = new activeobject ("Microsoft. XMLHTTP ");
}
}
Return XMLHTTP;
}
Function showhint (STR)
{
If (Str. Length = 0)
{
Document. getelementbyid ("suggestion"). innerhtml = "";
// Return;
}
 
XMLHTTP = getxmlhttpobject ();
If (XMLHTTP = NULL)
{
Alert ("Brower does not support HTTP request ");
// Return;
}
VaR url = "demo11.php? Q = "+ STR +" & id = "+ math. Random ();
// Alert (URL );
XMLHTTP. Open ("get", URL, true );
XMLHTTP. onreadystatechange = statechanged;
// Alert (XMLHTTP. readystate );
XMLHTTP. Send (null );
}
Function statechanged ()
{
If (XMLHTTP. readystate = 4)
{
// Alert (XMLHTTP. responsetext );
Document. getelementbyid ("suggestion"). innerhtml = XMLHTTP. responsetext;
}
}

</SCRIPT>
</Head>

<Body>
User name: <input type = "text" name = "user" id = "user" onkeyup = "showhint (this. Value)"/> <br/>
Suggestions: <span id = "suggestion"> </span>
</Body>
</Html>

 

Demo11.php

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> untitled document </title>
</Head>

<Body>
<? 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 ";
// Get the Q Parameter from URL
$ Q = $ _ Get ["Q"];
// Print_r ($ );
// Lookup all hints from array if length of q> 0
If (strlen ($ q)> 0)
{
// Echo count ($ );
$ Hint = "";
For ($ I = 0; $ I <count ($ A); $ I ++)
{
// Echo "the first line". strtolower ($ q). "<br> ";
// Echo "second line". substr ($ A [$ I], 0, strlen ($ Q). "<br> ";
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;
?>
</Body>
</Html>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.