接到了老师的一个作业,实现的布局如图:
如果输入了科室ID,科室名字只显示与ID对应的,若没有输入,则显示全部,然后根据I科室名字的值,在所属大科中的文本框自动显示科室名字所在的大科。例如:选择了心血管内科,则在所属大科显示内科。
主要代码如下:
根据ID请求科室
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function
showHint(str)
{
var
xmlhttp;
if
(window.XMLHttpRequest)
{
xmlhttp=
new
XMLHttpRequest();
}
else
{
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();
}
Copy after login
keshi.php:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!--?php
define(TEST,
"test"
);
include_once
"mysql.func.php"
;
connectMySQL();
selectDB();
setZiFuJi();
$q
=
$_GET
[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
.= '
Copy after login
'; $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 .= ''.$rowKSQ['name'].' '; } $hint .= ' '; } } // 不是数字 else { $resultDK = queryDB(select table_dake.name from table_dake); $hint = '科室名字:
'; while (!!$rowDK = fetchAssoc($resultDK)) { $hint .= ''; $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 .= ''.$rowKS['name'].' '; } $hint .= ' '; } } $response=$hint; //输出响应 echo $response; ?> 效果: 未输入ID如上图,输入ID在下图: 下一篇:百家搜索:在网站中添加Google、百度等搜索引擎