获取有关组合框更改的患者详细信息

时间:2014-07-29 10:42:50

标签: javascript php combobox

当我在组合框中选择pname时,我需要用于获取患者的addr和pnum的代码。

我该怎么做?

<script>

function getVal() {
document.getElementById("text2").value = document.getElementById("model").value;
}
</script>   

<body>
//code in opening and getting my addr and pnum in dbase

<?php
include('connect.php');
$pname=$_GET['tpname'];
$result = mysql_query("SELECT * FROM tblnpatient where pname='$pname'");

while($row = mysql_fetch_array($result))
    {
        $pnum=$row['pnum'];
        $addr=$row['addr']; 
}
?>

//code for choosing pname

<tr><td>Patient Name:
<div id="ma">
<select name="pname" class="textfields" id="model"  style="width:180px;"      onchange="getVal();">
<option id="0" >--Select Patient Name--</option>
 <?php
    $con=mysqli_connect("localhost","root","","dbnpatient");
        if (mysqli_connect_errno()){
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
    $pnum=$_GET['pnum'];
    $query = mysqli_query($con, "SELECT * FROM tblnpatient");
    while($row = mysqli_fetch_array($query)){
        $pnum = $row['pnum'];
        $pname = $row['pname'];
 ?>
 <option id="<?php echo $pnum; ?>" value="<?php echo $pname; ?>"><?php echo $pname; ?>  </option>
 <?php } ?>
</select>

//code for getting pname and addr

Address:<input type="text" name="ename" size="20"  id="ma" value="<?php echo $addr ?>"/>
Name:<input type="text" name="ename" size="20"  id="ma" value="<?php echo $pname ?>"/>

1 个答案:

答案 0 :(得分:0)

在你的while循环中添加以下内容(如果你有该列,否则用类似的东西替换) $ paddress = $ row [&#39; paddress&#39;];

您可以使用选项

中的数据属性存储所需信息
<option id="<?php echo $pnum; ?>" data-pname="<?php echo $pname; ?>" data-paddress="<?php echo $paddress; ?>" value="<?php echo $pname; ?>"><?php echo $pname; ?></option>

然后更改你的getVal()函数

function getVal() {
  var options = document.getElementById("model").options;
  if (options.selectedIndex != -1) {
    var addr = document.getElementById('paddress');
    var name = document.getElementById('pname');
    addr.value = options[options.selectedIndex].getAttribute('data-paddress');
    name.value = options[options.selectedIndex].getAttribute('data-pname');
  }
}

现在将地址和名称的输入字段的id更改为paddress和pname。重要的是要知道永远不会有重复的ID

我希望帮助