PHP+js+ajax,用于两个下拉之间切换数据

chuting1 2016-06-02 11:12:00
数据库表中

szarea oper
081 aaa
082 bbb
081 aaa1
081 aaa2
082 bbb1

程序要求选择081或082,显示在区域中,后面的oper随之变化,用AJAX+JS+PHP

我的代码如下:
login.php
<html>
<head>
</head>
<body bgcolor="#935612" background="0.jpg">

<table width="1000" align="center" border="0" cellspacing="1" cellpadding="0">
<tr>
<td width="500" colspan="2"><?PHP echo($Errmsg); ?></td>
</tr>
<form id="form1" name="form1" method="POST" action="<?PHP $_SERVER['PHP_SELF'] ?>" onsubmit="return CheckForm();">
<tr>
<td width="80">所在区域:</td>
<td width="417">
<script charset="gb2312" type="text/javascript" src="oper.js"></script>

<!-- <script type="text/javascript">
function showCustomer(str)
{
var xmlhttp;
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getoper.asp?q="+str,true);
xmlhttp.send();
}
</script> -->



<SELECT NAME="szarea" onChange="overnm(this.value)">
<?php
require "conf.php";
$connection=mysql_connect($host,$user,$pass) or die("unable to connect!");
mysql_select_db($db) or die("unable to select database ");
$query1="select distinct szarea from operatorinfo order by szarea";
$result=mysql_query($query1) or die("error sql to exec0");
if (mysql_num_rows($result)>0 )
{
while ( $row=mysql_fetch_row($result))
{
echo "<option value='" . $row[0] . "'>" . $row[0] . "</option>";
}
}
else
{
echo "no rows found !";
}
mysql_free_result($result);
?>
</SELECT>
</td>
</tr>
<tr>
<td width="80">用户名:</td>
<td width="417">
<SELECT NAME="usernm">

</SELECT>
</td>
</tr>
<tr>
<td>密  码:</td>
<td><input type="password" name="userpwd"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value=" 登 录 ">
<input type="hidden" name="chkpw" value="send">
<input type="button" name="Submit2" value=" 关 闭 " onclick="self.close()"></td>
</tr>
</form>

</table>
</HTML>


OPER.js
var xmlHttp

function overnm(str)
{
if (str.length==0)
{
document.getElementById("szarea").innerHTML=""
return
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="getoper.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("usernm").innerHTML=xmlHttp.responseText //responseXML responseText
}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}


getoper.php

<?php
header('Content-Type:text/html;charset=GB2312');
$q=$_GET["q"];
//echo $q;
require "conf.php";
$connection=mysql_connect($host,$user,$pass) or die("unable to connect!");
mysql_select_db($db) or die("unable to select database ");

$query1="select adminname from operatorinfo where szarea='" . $q . "' order by id";
//echo $query1;
$result=mysql_query($query1) or die("error sql to exec2");
if (mysql_num_rows($result)>0 )
{
$operlist = array();
$i=0;
echo "<select id='usernm'>";
echo "<script type='text/javascript'>";
echo "var usernm = document.getElementById ('usernm');";

while ( $row=mysql_fetch_row($result))
{

echo " var opt = document.createElement ('option' );";
echo " opt.value = '" . $row[0] . "';";
echo " opt.innerText = '" . $row[0] . "';";
echo " usernm.appendChild (opt);";
}
echo "</script>";
echo "</select>";

}


mysql_free_result($result);
?>


getoper.php?q=081可以直接输出下拉列表,但login.php就不可以,提示oper.js 28行错误
...全文
90 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
hch126163 2016-06-02
  • 打赏
  • 举报
回复
ajax 通常接受 json 数据 getoper.php 应该诊断 ajax 请求和正常请求,分别响应不同的 结果

87,921

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧