PHP+MySQL实现下拉框显示数据库信息
<html>
<head>
<title>Classroom research</title>
</head>
<body>
<h1>CRMS - Classroom research</h1>
<form action="research.php" method="post">
<table border="0">
<tr>
<td>Classroom ID</td>
<td><input type="text" name="Cno" maxlength="13" size="13"></td>
</tr>
<tr>
<td>Course ID</td>
<td>
<select>
<option value=0>--请选择--</option>
<?php
$con = mysql_connect("localhost","root","");
$search_course = "SELECT CID FROM course2";
$result = mysql_query($search_course, $con);
while($row = mysql_fetch_array($result))
{
<option value="$row">$row</option>
}
?>
</select>
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Register"></td>
</tr>
</table>
</form>
</body>
</html>
1、我希望在第一个框输入课室号,第二框通过下拉框显示数据库中course2表的CID的内容。
这应该就是不对的,毫无头绪,不知道怎么实现。求详细代码!
2、怎样实现填写其中一个数据(即不用填两个)就可以查询数据?
[解决办法]
仅供参考:
<?php/* Created on [2012-5-16] */#查询标题信息$sql="select * from table"; $res=mysql_query($sql); if(!$res) die("SQL: {$sql} <br>Error:".mysql_error()); if(mysql_affected_rows() > 0){ $titles = array(); while($rows = mysql_fetch_array(MYSQL_ASSOC)){ array_push($titles,$rows); } }?><table border=1><?php foreach($titles as $row_Recordset_task){ ?> <tr> <td> <a href="javascript:void(0)" onclick="record(<?=$row_Recordset_task['TID']?>)" > <?=$row_Recordset_task['csa_title']?> </a> </td> </tr><?php } ?></table><div id="show"></div><form name="frm"><select name="s1" onChange="redirec(this.value)"> <option selected>请选择</option> <option value="1">内科</option> <option value="2">内科</option> <option value="3">内科</option></select><div id="s2"></div></form><script>//Ajaxvar xmlHttp; function createXMLHttpRequest() { if(window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } else if (window.ActiveXObject) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } } function record(id){ createXMLHttpRequest(); url = "action.php?id="+id+"&ran="+Math.random(); method = "GET"; xmlHttp.open(method,url,true); xmlHttp.onreadystatechange = show; xmlHttp.send(null); } function show(){ if (xmlHttp.readyState == 4){ if (xmlHttp.status == 200){ var text = xmlHttp.responseText; document.getElementById("s2").innerHTML = text; }else { alert("response error code:"+xmlHttp.status); } } }</script><?php#action.phpif(isset($_GET['id'])){ $sql="select * from table where id=".$_GET['id']; $res=mysql_query($sql); if(!$res) die("SQL: {$sql} <br>Error:".mysql_error()); if(mysql_affected_rows() > 0){ $arrMenu=array(); while($rows = mysql_fetch_array(MYSQL_ASSOC)){ array_push($arrMenu,$rows); } } mysql_close(); if(!empty($arrMenu)){ echo "<select name='menu2'>"; foreach($arrMenu as $item2){ echo "<option value='{$item2['id']}'>{$item2['name']}</option>"; } echo "</select>"; }}?>
[解决办法]
就这么简单,不需要上面那么复杂!!伱直接复制过去就可以用了。。。。
<?php
//require_once('conn.php'); //伱最好写个连接数据库的文件 每次包含一下就行了! 而且要写在最上面
//最好把下面三行写在conn.php文件里 以后每次用时 向上面那样包含一下就OK了!!
$con = mysql_connect("localhost","root","***") or die("错误信息:".mysql_error()); //连接
$db = mysql_select_db("表course2所在的数据库名"); //这个要不写就取不着数据 但不会报错
mysql_query("set names gb2312");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<select>
<option>-请选择-</option>
<?php
$sql="select CID from course2";
$result=mysql_query($sql);
while($row=mysql_fetch_assoc($result)){
?>
<option value="<?php echo $row['CID'] ?>"><?php echo $row['CID'] ?></option> //这个值要用php的方法取出来
<?php
}
?>
</select>
</body>
</html>
[解决办法]
搞这么复杂?script标签在代码底部加也行啊,只要有:
<script>//Ajaxvar xmlHttp; function createXMLHttpRequest() { if(window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } else if (window.ActiveXObject) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } } function record(id){ createXMLHttpRequest();//指定目标地址及参数 url = "action.php?id="+id+"&ran="+Math.random(); method = "GET"; xmlHttp.open(method,url,true); xmlHttp.onreadystatechange = show; xmlHttp.send(null); } function show(){ if (xmlHttp.readyState == 4){ if (xmlHttp.status == 200){//回调函数,返回的后端结果 var text = xmlHttp.responseText; document.getElementById("s2").innerHTML = text; }else { alert("response error code:"+xmlHttp.status); } } }</script>