Ajax Asynchronous request PHP data, Ajax asynchronous php_php Tutorial

Source: Internet
Author: User

Ajax asynchronously requests PHP data, Ajax asynchronous PHP


Source: http://www.ido321.com/1138.html

Received a teacher's homework, the realization of the layout

If the department ID is entered, the department name only shows the corresponding ID, if there is no input, then display all, and then according to the name of the Department of I, in the section of the text box automatically display the department name of the major section. For example: Cardiovascular medicine was selected, and the Department of Internal Medicine was shown in its own major.

The main code is as follows:

Request department based on ID

function Showhint (str) {    var xmlhttp;    if (Window. XMLHttpRequest)     {//ie7+, Firefox, Chrome, Opera, Safari        xmlhttp=new XMLHttpRequest ();     }       Else     {//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","keshi.php?q="+str,true);    Xmlhttp.send ();}

keshi.php:

/  
    * Prevent malicious calls *
  /Define"TEST",' Test ');//Introduction fileinclude_once ' mysql.func.php ';//Database initializationConnectmysql (); Selectdb (); Setzifuji ();//Get the Q parameter from the URL$q =$_get["Q"];//If q is a numeric or numeric stringif(Is_numeric ($q))    {$q = Intval ($q); $hint =""; $resultDKQ = Querydb ("SELECT name from Table_dake where id= $q"); $hint =' Department Name:'; while (!! $rowDKQ = Fetchassoc ($resultDKQ)) {$hint. = '< span="">]. '      > '; $resultKSQ = Querydb ("Select Table_dake.id,table_keshi.sid,table_keshi.name from Table_dake,table_keshi where Table_      Dake.name= ' {$rowDKQ [' name ']} ' and Table_keshi.sid=table_dake.id '); while (!! $rowKSQ = Fetchassoc ($resultKSQ)) {$hint. = '' Name ']. '      > '. $rowKSQ [' name ']. ';    } $hint. = ';    }}//is not a number else {$resultDK = Querydb ("Select Table_dake.name from Table_dake"); $hint = ' Department Name:'; while(!! $rowDK = Fetchassoc ($resultDK)) {$hint. ='< span="">].      ' > '; $resultKS = Querydb ("Select Table_dake.id,table_keshi.sid,table_keshi.name from Table_dake,table_keshi where      Table_dake.name= ' {$rowDK [' name ']} ' and Table_keshi.sid=table_dake.id ');  while (!! $rowKS = Fetchassoc ($resultKS)) {$hint. = '' Name ']. ' >'. $rowKS [' name '].      ''; } $hint. = "; }} $response = $hint;//Output ResponseEcho$response;? >

Effect:

ID not entered as, enter ID in:

Next: Hundreds of search: in the site to add Google, Baidu and other search engines


After committing asynchronously with Ajax, how does PHP write the code to return the results of the processing to the client

Directly echo ' String ' in the S-end, or EXIT (Json_encode (array)); All right,

The Async of Ajax

Each request is not a problem, but there must be a delay in each request. The problem with Async is that there are delays, there is no question of receiving the return value, and the return value is definitely where you wrote it wrong.

At the same time you this situation my comparison suggested that the data are requested at a time, there is a client side, and then as necessary to display it.

Hope to help you!

http://www.bkjia.com/PHPjc/905905.html www.bkjia.com true http://www.bkjia.com/PHPjc/905905.html techarticle Ajax Asynchronous request PHP data, Ajax asynchronous PHP Source: Http://www.ido321.com/1138.html received a teacher's homework, the implementation of the layout if entered the Department ID, department name only ...

  • 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.