ajax的POST方法传值
发布时间:2020-12-16 00:36:29 所属栏目:百科 来源:网络整理
导读:今天接触ajax,通过一个小小的例子来加深对ajax的了解 通过index.php的下拉框选择班级,然后通过ajax的post传值给result.php,result.php通过传过来的班级名 到数据库中查找相应班级里的学生,再把学生信息返回给index.php。 本例已通过测试 ?php//从数据库
今天接触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" 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> </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."rn",FILE_APPEND); mysql_connect("localhost","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."rn",FILE_APPEND); echo $result; ?> 通过post传值要注意很多细节,否则很容易出错 目前我还没有解决中文乱码问题,如果有好心的前辈指点一下,万分感谢~ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |