日期:2014-05-16 浏览次数:20513 次
<?php
//从数据库把班级名称取出来放入下拉列表中
mysql_connect("localhost","root","root") or die("数据库连接失败");
mysql_select_db("studentmanage") or die("数据库不存在");
$sql="select * from class";
$rs=mysql_query($sql);
$info=array();
while($row=mysql_fetch_assoc($rs)){
$info[]=$row;
}
?>
<!doctype html>
<html>
<head>
<title>查询学生信息</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<style type="text/css">
</style>
<script type="text/javascript">
var xmlhttprequest;;
function getMessage(){
if(window.ActiveXObject){
xmlhttprequest=new ActiveXObject("Microsoft.XMLHTTP");
}else{
xmlhttprequest=new XMLHttpRequest();
}
if(xmlhttprequest){
var url="result.php";
var cid=document.getElementById("class").value;
var show=document.getElementById("info");
var data="class=cid";
xmlhttprequest.open("post",url,true);
xmlhttprequest.setRequestHeader("content-type","application/x-www-form-urlencode");
xmlhttprequest.onreadystatechange=function(){
if(xmlhttprequest.readyState==4){
if(xmlhttprequest.status==200){
var info=xmlhttprequest.responseText;
if(info==""){
show.value="你好,你查询的班级暂时还没有学生";
}else{
show.value=info;
}
}
}else{
alert("error");
}
}
xmlhttprequest.send(data);
}
}
</script>
</head>
<body>
<div id="content">
<h1>
请选择班级:
<select name="class" id="class" onchange="getMessage()">
<option value="" selected>choose class</option>
<?php foreach($info as $v){?>
<option value="<?php echo $v['cid'];?>"><?php echo $v['cname'];?></option>
<?php }?>
</select>
</h1>
<div id="info"></div>
</div>
</body>
</html>
<?php
//header("content-type:text/html;charset=utf-8");
header("Content-type:text/xml;charset=utf-8");
header("Cache-Control:no-cache");
$class=$_POST['class'];
mysql_connect("localhost","root","root") or die("数据库连接失败");
mysql_select_db("studentmanage");
mysql_query("set names utf-8");
$sql="select * from student where cid='$class'";
//$sql="select * from student where cid=3";
$rs=mysql_query($sql);
$info=array();
while($row=mysql_fetch_assoc($rs)){
$info[]=$row;
}
$result="<table><tr><td>学号</td><td>姓名</td><td>住址</td><td>班级</td></tr>";
foreach($info as $v){
$result.="<tr><td>{$v['sid']}</td><td>{$v['name']}</td><td>{$v['address']}</td><td>{$v['cid']}</td></tr>";
}
$result.="</table>";
echo $result;
?>