不会ajax,请教一下我做的这个页面,不刷新从数据库读取数据的话应该怎么写,会ajax的兄弟帮我看看。用url传值和POST传值都解决不了我的问题呢,看来只能寄希望于ajax了。想了一下午了,没搞明白,望不吝赐教。
<?php
include('include/dbconnect.php');//数据库连接文件
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>更换客房</title>
<link rel="stylesheet" type="text/css" href="style.css">
<style type="text/css">
<!--
.STYLE1 {color: #FFFFFF}
.STYLE2 {font-size: 12px}
.STYLE4 {font-size: 13px}
.STYLE5 {font-family: "华文琥珀"; font-size: 20px;}
-->
</style>
</head>
<body bgcolor="#7791EA">
<table width="500" height="60" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="center" class="STYLE5">更换客房</td>
</tr>
</table>
<table width="500" height="25" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#9ABADA">
<tr>
<td>
<table width="500" height="25" border="0" align="center" cellpadding="0" cellspacing="1">
<form name="form1" id="form1" method="post" action="main_index.php?lm=换房1">
<tr>
<td><div align="center">客房类型:在这里选取客房类型,不刷新页面读取数据库数据
<select name="style" size="1" id="style">
<?php $query="select * from style";
$result=mysql_query($query);
if($result==true){
while($myrow=mysql_fetch_array($result)){
?> <option><?php echo $myrow[stylename]; $_SESSION["res_style"]=$myrow[stylename];?></option>
<?php
}
}
?>
</select> <input type="submit" name="submit" value="查询" class="buttoncss"></div>
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
<table width="500" height="10" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td></td>
</tr>
</table>
<?php
if($_POST[submit]!="")
{
?>
<table width="500" height="50" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="#7791EA" valign="middle">
<table width="500" height="50" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#9ABADA">
<tr>
<td height="25"></td>
<td width="100" height="25"><div align="center" class="STYLE1">客房号码</div></td>
<td width="120"><div align="center" class="STYLE1">客房类型</div></td>
<td width="120"><div align="center" class="STYLE1">客房状态</div></td>
<td width="100"><div align="center" class="STYLE1">容纳人数</div></td>
</tr>
<?php
$style=$_POST[style];
$sql=mysql_query("select * from roomlist where `style`='$style' AND `condition`='空闲' ORDER BY roomnum");
$rs=mysql_fetch_array($sql);
if($rs==false)
{
?>
<tr>
<td height="25" colspan="5" bgcolor="#FFFFFF"><div align="center">没有查找到任何记录!</div></td>
</tr>
<?php
}
else
{
do{
?>
<tr>
<form name="myform" action="room_change_ok.php" method="post" id="myform"> <!--显示查询结果-->
<td align="center" bgcolor="#FFFFFF"><span class="STYLE2">
<input type="radio" name="change_roomnum" value="<?php echo $rs[roomnum];?>">
</span></td>
<td height="25" bgcolor="#FFFFFF"><div align="center"><?php echo $rs[roomnum];?></div></td>
<td height="25" bgcolor="#FFFFFF"><div align="center"><?php echo $rs[style];?></div></td>
<td height="25" bgcolor="#FFFFFF"><div align="center"><?php echo $rs[condition];?></div></td>
<td height="25" bgcolor="#FFFFFF"><div align="center"><?php echo $rs[capacities];?></div></td>
</tr>
<?php
}while($rs=mysql_fetch_array($sql));
}
?>
<tr>
<td colspan="5" height="25" bgcolor="#FFFFFF"><div align="right"><input type="submit" value="换房" name="Submit" /></div></td>
</tr>
</form>
</table>
</td>
</tr>
</table>
<?php
}
?>
<table height="100">
<tr><td></td></tr>
</table>
</html>
<?php
include('include/dbconnect.php');//数据库连接文件
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>更换客房</title>
<link rel="stylesheet" type="text/css" href="style.css">
<style type="text/css">
<!--
.STYLE1 {color: #FFFFFF}
.STYLE2 {font-size: 12px}
.STYLE4 {font-size: 13px}
.STYLE5 {font-family: "华文琥珀"; font-size: 20px;}
-->
</style>
</head>
<body bgcolor="#7791EA">
<table width="500" height="60" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="center" class="STYLE5">更换客房</td>
</tr>
</table>
<table width="500" height="25" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#9ABADA">
<tr>
<td>
<table width="500" height="25" border="0" align="center" cellpadding="0" cellspacing="1">
<form name="form1" id="form1" method="post" action="main_index.php?lm=换房1">
<tr>
<td><div align="center">客房类型:在这里选取客房类型,不刷新页面读取数据库数据
<select name="style" size="1" id="style">
<?php $query="select * from style";
$result=mysql_query($query);
if($result==true){
while($myrow=mysql_fetch_array($result)){
?> <option><?php echo $myrow[stylename]; $_SESSION["res_style"]=$myrow[stylename];?></option>
<?php
}
}
?>
</select> <input type="submit" name="submit" value="查询" class="buttoncss"></div>
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
<table width="500" height="10" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td></td>
</tr>
</table>
<?php
if($_POST[submit]!="")
{
?>
<table width="500" height="50" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="#7791EA" valign="middle">
<table width="500" height="50" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#9ABADA">
<tr>
<td height="25"></td>
<td width="100" height="25"><div align="center" class="STYLE1">客房号码</div></td>
<td width="120"><div align="center" class="STYLE1">客房类型</div></td>
<td width="120"><div align="center" class="STYLE1">客房状态</div></td>
<td width="100"><div align="center" class="STYLE1">容纳人数</div></td>
</tr>
<?php
$style=$_POST[style];
$sql=mysql_query("select * from roomlist where `style`='$style' AND `condition`='空闲' ORDER BY roomnum");
$rs=mysql_fetch_array($sql);
if($rs==false)
{
?>
<tr>
<td height="25" colspan="5" bgcolor="#FFFFFF"><div align="center">没有查找到任何记录!</div></td>
</tr>
<?php
}
else
{
do{
?>
<tr>
<form name="myform" action="room_change_ok.php" method="post" id="myform"> <!--显示查询结果-->
<td align="center" bgcolor="#FFFFFF"><span class="STYLE2">
<input type="radio" name="change_roomnum" value="<?php echo $rs[roomnum];?>">
</span></td>
<td height="25" bgcolor="#FFFFFF"><div align="center"><?php echo $rs[roomnum];?></div></td>
<td height="25" bgcolor="#FFFFFF"><div align="center"><?php echo $rs[style];?></div></td>
<td height="25" bgcolor="#FFFFFF"><div align="center"><?php echo $rs[condition];?></div></td>
<td height="25" bgcolor="#FFFFFF"><div align="center"><?php echo $rs[capacities];?></div></td>
</tr>
<?php
}while($rs=mysql_fetch_array($sql));
}
?>
<tr>
<td colspan="5" height="25" bgcolor="#FFFFFF"><div align="right"><input type="submit" value="换房" name="Submit" /></div></td>
</tr>
</form>
</table>
</td>
</tr>
</table>
<?php
}
?>
<table height="100">
<tr><td></td></tr>
</table>
</html>
index.php
<input type='button' value='click' id='btnclick' onclick='getValue()'>
<script>
var xmlHttp;
function createXMLHttpRequest(){
if(window.XMLHttpRequest){
var xmlHttp=new XMLHttpRequest();
if(xmlHttp.overrideMimeType){
xmlHttp.overrideMimeType("text/xml");
}
}else{
try{
xmlHttp = new XMLHttpRequest();
}catch(e){
var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
'MSXML2.XMLHTTP.5.0',
'MSXML2.XMLHTTP.4.0',
'MSXML2.XMLHTTP.3.0',
'MSXML2.XMLHTTP',
'Microsoft.XMLHTTP');
for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++){
try{
xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
}
catch (e) {
}
}
}
}
if (!xmlHttp){
alert("Error creating the XMLHttpRequest object.");
}else{
return xmlHttp;
}
}
function getValue(){
var url="./test.php"
xmlHttp=createXMLHttpRequest();
xmlHttp.open("POST", url, true);
xmlHttp.onreadystatechange =function(){
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
document.getElementById('btnclick').value=xmlHttp.responseText;
}
}}
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send("value=Test");
} </script>test.php
<?php
if($_POST['value']=='Test'){
echo $_POST['value'];
}?>
获得test.php文件的内容,内容是什么,你自己写,
要怎么改,
改这里就行document.getElementById('btnclick').value=xmlHttp.responseText;