Php-ajax and PHP

Source: Internet
Author: User

AJAX is used to create more interactive applications.

AJAX PHP Instance

The following example shows how a Web page communicates with a Web server when the user types characters in the input box:

Example explanation-HTML page

The "Showhint ()" function is executed when the user types a character in the input box above. This function is triggered by the "onkeyup" event:

  1. <script>
    functionShowhint(Str)
    {
    If (Str.Length==0)
    {
    Document.getElementById("Txthint").InnerHTML="";
    Return;
    }
    If (Window.XMLHttpRequest)
    {Code for ie7+, Firefox, Chrome, Opera, Safari
    xmlHTTP=New XMLHttpRequest();
    }
    Else
    {Code for IE6, IE5
    xmlHTTP=New ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlHTTP.onReadyStateChange=function()
    {
    If (xmlHTTP.ReadyState==4 &&xmlHTTP.Status==200)
    {
    Document.getElementById("Txthint").InnerHTML=xmlHTTP.ResponseText;
    }
    }
    xmlHTTP.Open("GET","Gethint.php?q="+Str,True);
    xmlHTTP.Send();
    }
    </script>
    <body>

    <p><b>Enter a name in the input box:</b></p>
    <form>
    Name:<input type= "text" onkeyup= "showhint< Span class= "pun" > (this. Value ">
    </ Form>
    <p> return value: <span id= "txthint" ></span ></p>

    </BODY>
    </HTML>
Copy

Source Code Explanation:

If the input box is empty (str.length==0), the function empties the contents of the Txthint placeholder and exits the function.

If the input box is not empty, then Showhint () performs the following steps:

    • Create a XMLHttpRequest Object
    • Create a function that executes when the server responds ready
    • Send a request to a file on the server
    • Note the parameter (q) added to the end of the URL (contains the contents of the input box)
PHP file

The above server page that was called by JavaScript is a php file named "gethint.php".

The source code in "gethint.php" checks the name array and returns the corresponding name to the browser:

  1. <?Php
    Populate a name in an array
    $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 the request URL address
    $q=$_get["Q"];

    Find if the value is matched if 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];
    }
    }
    }
    }

    If no match value is set output to "no suggestion"
    Or to the correct values
    if ( $hint == )
    {
    $response = "no suggestion" }
    else
    {
    $response = $hint }

    //output return value
    ?>
Copy

Explanation: If JavaScript sends any text (that is, strlen ($q) > 0), it will occur:

    1. Find names that match the characters sent by JavaScript
    2. If no match is found, the response string is set to "no suggestion"
    3. If one or more matching names are found, the response string is set with all names
    4. Send response to "txthint" placeholder
PHP Ajax cross-domain Problem solving solution

If your asynchronous request needs to be cross-domain viewable: PHP Ajax cross-domain problem solving solution.

Php-ajax and PHP

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.