现在的代码,已在最后附上。我想添加办公室名称的话,直接用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>
但是,这里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>
<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>
</tr>
</table>
<input type="submit" value="提交" />
</div>
</form>
可能是我的描述有点问题。。
还有我这个本来是一个注册页面 前面还有填写姓名什么的没列出来。。
insertinto useroffice() values('$name','$sex','$age','$officename','officecode')
如果这样的话我在注册的时候
<option value="<?=$result['cod']?>"><?=$result['officename']?></option>
在insert into的时候,他的办公室名称 就变成办公室号码了 这样的话 还需要怎么样改进呢? 谢谢
感谢指导 已经完成我需要的要求我在执行insert into之前
$sql1="select * from officename where cod='$officecode'
$result1=mysql_query($sql1);
$row=mysql_fetch_assoc($result1);
$useroffice=$row['officename'];
然后insert过去的时候是
把values中的 $officename 替换为 $useroffice