Ajax非同步請求PHP資料

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   color   ar   os   sp   

來源:http://www.ido321.com/1138.html

接到了老師的一個作業,實現的布局

如果輸入了科室ID,科室名字只顯示與ID對應的,若沒有輸入,則顯示全部,然後根據I科室名字的值,在所屬大科中的文字框自動顯示科室名字所在的大科。例如:選擇了心血管內科,則在所屬大科顯示內科。

主要代碼如下:

根據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:

<?php/*防止惡意調用*/define("TEST",‘test‘);// 引入檔案include_once ‘mysql.func.php‘;// 資料庫初始化connectMySQL();selectDB();setZiFuJi();//獲得來自 URL 的 q 參數$q=$_GET["q"];//如果 q 是數字或者數字字串if (is_numeric($q))  {    $q = intval($q);    $hint="";    $resultDKQ = queryDB("select name from table_dake where id=$q");    $hint = ‘科室名字:<select name="ksname" id="ksname" onchange="show(this.options[this.selectedIndex].value)">‘;    while (!!$rowDKQ = fetchAssoc($resultDKQ))    {      $hint .= ‘<optgroup label=‘.$rowDKQ[‘name‘].‘>‘;      $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 .= ‘<option style="padding-left:15px;" value =‘. $rowKSQ[‘name‘].‘>‘.$rowKSQ[‘name‘].‘</option>‘;      }      $hint .= ‘</optgroup>‘;    }  }  // 不是數字  else  {    $resultDK = queryDB("select table_dake.name from table_dake");    $hint = ‘科室名字:<select name="ksname" id="ksname" onchange="show(this.options[this.selectedIndex].value)">‘;     while (!!$rowDK = fetchAssoc($resultDK))    {      $hint .= ‘<optgroup label=‘.$rowDK[‘name‘].‘>‘;      $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 .= ‘<option style="padding-left:15px;" value =‘. $rowKS[‘name‘].‘>‘.$rowKS[‘name‘].‘</option>‘;      }      $hint .= ‘</optgroup>‘;    }  } $response=$hint;//輸出響應echo $response;?>

 

效果:

未輸入ID如,輸入ID在:

下一篇:百家搜尋:在網站中添加Google、百度等搜尋引擎

Ajax非同步請求PHP資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.