简体   繁体   中英

How do I populate textbox from a combobox selected

i want a combobox which populates itself from a database column, when a user selects an option from the combobox it will populate textboxes, which are on the same page, with information related to the selection in the combobox.

Below is the code I used to populate a textbox when I selected the select nama from the dropdown combobox. but it doesnt work, the textbox still blank. I have the drop down working and also it fills a JavaScript array using names but I just cannot work out how to use the array to show in the fields.

i understand it will have to use javascript and Onchange function, can anyone point me in the right direction?

cheers and thank in advance :)

<script src="js/jquery.js"></script>
    <script type="text/javascript">
            var selected = new Array();

            <?php
                $query1 = "SELECT * FROM inventori";
                $result1 = mysql_query($query1) or die(mysql_error());

                // build javascript array
                while($row1=mysql_fetch_array($result1)){ 
                    echo 'selected['.$row1['selectnama'].'] = new Array();';
                    echo 'selected['.$row1['selectnama'].']["NAMA_BAHAN_BAKU"] = "'.$row1['NAMA_BAHAN_BAKU'].'";';
                    echo 'selected['.$row1['selectnama'].']["SATUAN"] = "'.$row1['SATUAN'].'";';
                }
            ?>

            function showname() {
                var selectnama = document.formInventori.selectnama.value;
                document.formInventori.SATUAN.value = selected[selectnama]["SATUAN"];
            }

            window.onload=function() {
                showname();
            } 

        </script>

    <form role="form" action="" method="post" name="formInventori">
          <div class="form-group">
               <label for="NAMA_BAHAN_BAKU">Nama Bahan Baku</label>
               <select class="form-control" name="selectnama" onchange="showname()">
               <option value="">Pilih Nama Bahan Baku</option>
               <?php
                   $query1 = "SELECT * FROM inventori";
                   $result1 = mysql_query($query1) or die(mysql_error());
                   // build javascript array
                   while($row1=mysql_fetch_array($result1)){ 
                        echo '<option value="'.$row1['ID_BAHAN_BAKU'].'">'.$row1['NAMA_BAHAN_BAKU'].'</option>';     }
               ?>
               </select>
          </div>
          <div class="form-group">
               <label for="SATUAN">Satuan Barang</label>
               <input type="text" class="form-control" readonly="" id="SATUAN" name="SATUAN" value="">
          </div>
</form>

我会使用 AJAX 来做到这一点,我会保存从组合框中选择的值,然后通过 ajax 将它传递给 php,这样 php 就会执行一个查询,其中所选值是搜索“键”,然后通过 php 打印结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM