PHP and Ajax get 123 of cases

Source: Internet
Author: User

Colleagues asked me, we get data from the database, the Ajax way to display the front page. Don't say anything, write a case.

1, create a page:

<!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head>    <Scripttype= "Text/javascript"src= "Ajax.js"></Script>    <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">    <title>Ajax Demo</title></Head><Body>    <inputtype= "button"ID= "Txt1"onclick= "Showhint ()"value= "Get 123"/>    <P>Digital:<spanID= "Txthint"></span></P> </Body></HTML>

2. Write a JS, which is Ajax

1 varXMLHTTP;2 functionShowhint ()3 {4     //defining a new array5     varArray =NewArray ();6     //Create a XMLHttpRequest object7     if(window. XMLHttpRequest)8{//code for ie7+, Firefox, Chrome, Opera, Safari modern browser9xmlhttp=NewXMLHttpRequest ();Ten     } One     Else A{//code for IE6, IE5 user low version IE -xmlhttp=NewActiveXObject ("Microsoft.XMLHTTP"); -     } the      -     //set the request type, request address, and whether to enable asynchronous processing requests, most settings turn on True -Xmlhttp.open ("GET", "gethint.php",true); -     //sending requests to the server + xmlhttp.send (); -     //Handling onReadyStateChange Events We specify the tasks that are performed when the server responds to readiness to be processed +Xmlhttp.onreadystatechange=function() A     { at         if(Xmlhttp.readystate==4 && xmlhttp.status==200) -         { -document.getElementById ("Txthint"). innerhtml=Xmlhttp.responsetext; -         } -     } -}

3, OK, write a PHP bar, as a server. The data is false.

1<?PHP2 //populate an array with a name3 $a[]= "1";4 $a[]= "2";5 $a[]= "3";6 7 $hint="";8  for($i= 0;$i<Count($a);$i++)9 {    Ten     $hint=$hint."  ".$a[$i]; One } A $response=$hint; - //Output Response - Echo $response; the?>

Look at the interface:

Click the button:

No more than a few more, come out.

PHP and Ajax get 123 of cases

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.