我有从MySQL数据库填充的texbox和下拉列表。我想从数据库中检索数据,并希望使用下拉列表选择下拉列表显示在文本框中,而无需刷新页面。这是我的代码,在此先感谢。

<select name="select1" class="form-control" id="dropdownlist1">
      <option id="0">-- Select the Company --</option>
      <?php
          require("dbcon.php");

          $getallcompanies = mysql_query("SELECT * FROM ifcandetails6");

          while($viewallcompanies = mysql_fetch_array($getallcompanies)){
              ?>
      <option id="<?php echo $viewallcompanies['tcuid']; ?>"><?php echo $viewallcompanies['tcname'] ?></option>
      <?php
          }
           ?>

  </select>


输入文字框:

 <input type="text" id="field1" value="<?php echo $viewallcompanies['tccontact']?>" disabled/>

最佳答案

像这样将事情分开:

<?php

require("dbcon.php");

$query = mysql_query("SELECT tcuid, tcname, tccontact FROM ifcandetails6");
while($row = mysql_fetch_array($query)){
    $contacts[] = $row['tccontact'];
    $companies[] = $row;
}
?>

<select name="select1" class="form-control" id="dropdownlist1">
    <option id="0">-- Select the Company --</option>
    <?php foreach($companies as $company) { ?>
        <option id="<?= $company['tcuid']; ?>"><?= $company['tcname'] ?></option>
    <?php }  ?>
</select>
<?php foreach($contacts as $contact) { ?>
    <input type="text" id="field1" value="<?= $contact['tccontact']?>"/>
<?php }  ?>

关于javascript - 使用php和mysql中的下拉选择列表从数据库中检索数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31236009/

10-10 00:17
查看更多