日期:2014-05-17  浏览次数:20504 次

如何通过数据库来实现对应数据的输出(描述的可能有点问题,内详)?
现在的代码,已在最后附上。我想添加办公室名称的话,直接用insert into进officename那个表就可以了
但是,这里officename与officecode是通过JS写死了。如果这样添加的话,两者的对应就有问题了,新添加的数据,是没有对应的officecode的,这就需要手动在js代码里添加,感觉这样很不合理。

所以我想的是不通过js,而是直接从数据库读取,officename表中共建立两个字段name与code 
直接通过读取数据库,将两者来进行对应,这样是否可行。需要怎样操作,最好有实例,谢谢

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<head>
<script language="javaScript">
 function setofficecode()
{
    //获取officename/officecode的值
  var officename = document.getElementById("officename").value;
  var o = document.getElementById("officecode");
   //条件判断
   if(officename == "XX办公室"){
o.value="101";
   }
   else if(officename == "YY办公室"){
o.value="201";
   }
   else if(officename == "ZZ办公室"){
o.value="301";
   }

 }
</script>
</head>


<form action="study3.php" method="post">
<div align="center">
<table>
  <tr>
  <td>办公室名称</td>
  <td>
  <select name="officename" id="officename" onchange="setofficecode()" style="width:150px;">
<?php
include('conn.php');
 $sql="select * from officename";
 $query=mysql_query($sql);
 while($result=mysql_fetch_array($query)){
  ?>
 <option value="<?=$result[officename]?>"><?=$result[officename]?></option>
 <?php
 }
 ?>
  </select>
  </td>
  </tr>
  <tr>
  <td>办公室号码</td>
  <td><input type="text" name="officecode" id="officecode" style="width:150px;" value="101"  /></td>
  </tr>
</table>

<input type="submit" value="提交" />
</div>


</form>

------解决方案--------------------
改好了
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 
<head>
</head>
 
 
<form action="study3.php" method="post">
<div align="center">
<table>
  <tr>
  <td>办公室名称</td>
  <td>
  <select name="officename" id="officename" onchange="document.getElementById('officecode').value=this.value" style="width:150px;">
<?php
include('conn.php');
 $sql="select * from officename";
 $query=mysql_query($sql);
 while($result=mysql_fetch_array($query)){
  ?>
 <option value="<?=$result['cod']?>"><?=$result['officename']?></option>
 <?php
 }
 ?>
  </select>
  </td>
  </tr>
  <tr>
  <td>办公室号码</td>
  <td><input type="text" name="officecode" id="officecode" style="width:150px;" value="101"  /></td>
  <