Table of Contents
通过ajax POST传值,但返回为空解决思路
Jun 13, 2016 am 11:54 AM
gt
info
lt
nbsp
通过ajax POST传值,但返回为空
第一次接触ajax,想通过一个简单的例子来练练
index.php中有一个下拉列表,选择不同的班级返回不同的学生信息
代码如下:
<?php<br /> //从数据库把班级名称取出来放入下拉列表中<br /> mysql_connect("localhost","root","root") or die("数据库连接失败");<br /> mysql_select_db("studentmanage") or die("数据库不存在");<br /> <br /> $sql="select * from class";<br /> $rs=mysql_query($sql);<br /> $info=array();<br /> while($row=mysql_fetch_assoc($rs)){<br /> $info[]=$row;<br /> }<br /><br />?><br /><!doctype html><br /><html><br /> <head><br /> <title>查询学生信息</title><br /> <meta http-equiv="content-type" content="text/html;charset=utf-8"/><br /> <style type="text/css"><br /> <br /> </style><br /> <script type="text/javascript"><br /> var xmlhttprequest;;<br /> function getMessage(){<br /> <br /> if(window.ActiveXObject){<br /> xmlhttprequest=new ActiveXObject("Microsoft.XMLHTTP");<br /> }else{<br /> xmlhttprequest=new XMLHttpRequest();<br /> }<br /> <br /> if(xmlhttprequest){<br /> var url="result.php";<br /> var cid=document.getElementById("class").value;<br /> var show=document.getElementById("info");<br /> var data="class=cid";<br /> xmlhttprequest.open("post",url,true);<br /> xmlhttprequest.setRequestHeader("content-type","application/x-www-form-urlencode");<br /> xmlhttprequest.onreadystatechange=function(){<br /> if(xmlhttprequest.readyState==4){<br /> if(xmlhttprequest.status==200){<br /> var info=xmlhttprequest.responseText;<br /> if(info==""){<br /> show.value="你好,你查询的班级暂时还没有学生";<br /> }else{<br /> show.value=info;<br /> }<br /> }<br /> }else{<br /> alert("error");<br /> }<br /> }<br /> xmlhttprequest.send(data);<br /> }<br /> }<br /> </script><br /> </head><br /> <body><br /> <div id="content"><br /> <h1 id="br-请选择班级-br-select-nbsp-name-class-nbsp-id-class-nbsp-onchange-getMessage-br-option-nbsp-value-nbsp-selected-choose-nbsp-class-option-br-php-nbsp-foreach-info-nbsp-as-nbsp-v-br-option-nbsp-value-php-nbsp-echo-nbsp-v-cid-php-nbsp-echo-nbsp-v-cname-option-br-php-nbsp-br-select-br"><br /> 请选择班级:<br /> <select name="class" id="class" onchange="getMessage()"><br /> <option value="" selected>choose class</option><br /> <?php foreach($info as $v){?><br /> <option value="<?php echo $v['cid'];?>"><?php echo $v['cname'];?></option><br /> <?php }?><br /> </select><br /> </h1><br /> <div id="info"></div><br /> </div><br /> </body><br /></html>
Copy after login
通过result.php查询数据库,并返回学生信息,代码如下:
<?php<br /> //header("content-type:text/html;charset=utf-8");<br /> header("Content-type:text/xml;charset=utf-8");<br /> header("Cache-Control:no-cache");<br /> $class=$_POST['class'];<br /> <br /> mysql_connect("localhost","root","root") or die("数据库连接失败");<br /> mysql_select_db("studentmanage");<br /> mysql_query("set names utf-8");<br /> $sql="select * from student where cid='$class'";<br /> //$sql="select * from student where cid=3";<br /> $rs=mysql_query($sql);<br /> $info=array();<br /> while($row=mysql_fetch_assoc($rs)){<br /> $info[]=$row;<br /> }<br /> <br /> $result="<table><tr><td>学号</td><td>姓名</td><td>住址</td><td>班级</td></tr>";<br /> foreach($info as $v){<br /> $result.="<tr><td>{$v['sid']}</td><td>{$v['name']}</td><td>{$v['address']}</td><td>{$v['cid']}</td></tr>";<br /> }<br /> $result.="</table>";<br /> echo $result;<br />?>
Copy after login
我单独测试了下result.php,我确定没有问题,能从数据库中取出对应的信息,但是就是不能把取到的信息返回到index.php中
求各位帮帮忙,找找问题在哪

------解决方案--------------------
给div设置value?即使你的查询时正常的,由于div的value并不会直接在视图中展现,所以你看不到效果。
你需要的应该是innerHTML
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution: Your organization requires you to change your PIN

How to adjust window border settings on Windows 11: Change color and size

How to change title bar color on Windows 11?

How to enable or disable taskbar thumbnail previews on Windows 11

OOBELANGUAGE Error Problems in Windows 11/10 Repair

What are the differences between Huawei GT3 Pro and GT4?

10 Ways to Adjust Brightness on Windows 11
