ajax的POST方法传值

今天接触ajax,通过一个小小的例子来加深对ajax的了解

通过index.php的下拉框选择班级,然后通过ajax的post传值给result.php,result.php通过传过来的班级名

到数据库中查找相应班级里的学生,再把学生信息返回给index.php。

本例已通过测试

<?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">
			#content{
				margin:0 auto;
				width:600px;
				text-align:center;
			}
			h3{
				background-color:black;
				color:#fff;
			}
		</style>
		<script type="text/javascript">
			
			function getMessage(){
				var xmlhttprequest;
				if(window.ActiveXObject){
					try{
						xmlhttprequest=new ActiveXObject("Microsoft.XMLHTTP");						
					}catch(e){
						try{
							xmlhttprequest=new ActiveXObject("Msxml2.XMLHTTP");
						}catch(e){}
					}
				}else{
					xmlhttprequest=new XMLHttpRequest();
				}
				
				if(xmlhttprequest){
					var url="result.php";
					var cid=document.getElementById("class").value;
					//window.alert(cid);
					var show=document.getElementById("info");
					//window.alert(show);
					var data="class="+cid;
					//window.alert(data);
					xmlhttprequest.open("post",url,true);
					xmlhttprequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
					
					//window.alert(url+data);
					
					xmlhttprequest.onreadystatechange=function(){
						if(xmlhttprequest.readyState==4){
							if(xmlhttprequest.status==200){
								var info=xmlhttprequest.responseText;
								if(info==""){
									show.innerHTML="你好,你查询的班级暂时还没有学生";
								}else{
									show.innerHTML=info;
								}
							}
						}else{
							//window.alert("error");
						}
					}
					xmlhttprequest.send(data);
				}else{
					window.alert("不能创建xmlhttprequest对象");
				}
			}
		</script>
	</head>
	<body>
		<div id="content">
			<h3>
				请选择班级:
				<select name="class" id="class" οnchange="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>
			</h3>
			<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'];
	
	file_put_contents("D:/WWW/PHPDemo/log.log",$class."\r\n",FILE_APPEND);
	
	
	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 style='border:solid 1px;width:600px;'><tr><th>学号</th><th>姓名</th><th>住址</th><th>班级</th></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>";
	file_put_contents("D:/WWW/PHPDemo/log.log",$result."\r\n",FILE_APPEND);
	echo $result;
?>

通过post传值要注意很多细节,否则很容易出错

目前我还没有解决中文乱码问题,如果有好心的前辈指点一下,万分感谢~

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值