当前位置: 代码迷 >> PHP >> PHP+MySQL实现下拉框显示数据库信息解决思路
  详细解决方案

PHP+MySQL实现下拉框显示数据库信息解决思路

热度:158   发布时间:2012-05-28 17:59:54.0
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 code

<?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>
//Ajax
var 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.php
if(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>";
    }
}

?>
 
  相关解决方案