標籤: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資料