当选择选项时,将数据从数据库添加到不可编辑字段


Adding data from database to non editable field when option is chosen

我想做的是从选项字段中选择名称,然后在不可编辑字段中显示用户的id。当我选择username,然后在不可编辑字段中显示用户id。这是php中的html:

<form id="reguserform" method="post" action="register.php#err">
<?php
    $result = getUnregisteredEmployee();    
    $result2 = getStatus();
?>
<!-- Username field -->
<select class="empidselectbox" name="username">
  <option disabled selected>Username (required)</option>
    <?php
      while ($row = mysql_fetch_array($result))
      {
        echo "<option value='".$row[0]."'>$row[0]</option>";
      }
      mysql_free_result($result);
    ?>  
</select>
<!-- Id field -->
<input type="hidden" name="id" value="ID" />
<input type="text" class="input name" placeholder="ID" name="id" readonly />
<center><input type="submit" class="button small" name="submit" value="Register User"/></center>
</form>

下面是我的查询:

<?php
function getUnregisteredEmployee()
{
    global $con;
    $sql = "SELECT username, uid FROM `user`";
    return @mysql_query($sql,$con);
}
?>

您的文件应该包含以下内容:

<select onchange="id_lookup(this.value)"><option></option></select>
<div class="searchresults"></div>
<script> 
function id_lookup(id){ 
xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET","id-lookup.php?id="+id, false); xmlhttp.send(null); document.getElementById("searchresults").value=xmlhttp.responseText; } 
</script>

id-lookup.php:

<?php
// establish connection - add yourself
// run sql and bind the $_GET["id"]
// echo id fields including html input fields
?>

你必须使用Jquery/JavaScript Ajax函数onChange事件来获得所需的输出

下面是jquery Ajax函数:http://api.jquery.com/jquery.ajax/