Php 基于带有附加函数的下拉列表自动填充字段

Php 基于带有附加函数的下拉列表自动填充字段,php,jquery,html,css,mysql,Php,Jquery,Html,Css,Mysql,我的这一行带有id为的文本字段,它可以使用“添加”按钮追加另一行。当追加新行时,追加的字段将自动生成唯一id。在第一列中,我有一个选项显示数据库表产品中的值,当他们从中选择值时,这将填充当前行中的其他字段 我的问题是,如何填充他们要填充的当前行上的字段 这就是我到目前为止所做的 <table class="table table-bordered" id="dynamic_field">

我的这一行带有id为的文本字段,它可以使用“添加”按钮追加另一行。当追加新行时,追加的字段将自动生成唯一id。在第一列中,我有一个选项显示数据库表产品中的值,当他们从中选择值时,这将填充当前行中的其他字段

我的问题是,如何填充他们要填充的当前行上的字段

这就是我到目前为止所做的

<table class="table table-bordered" id="dynamic_field">
                                                <tr>
                          <?php $drp = mysqli_query($conn,"SELECT * FROM products"); ?>

                          <td>
                            <select id="1" name="productcode[]" placeholder="Product Code" class="form-control name_list" required  value="">
                              <?php $pc = mysqli_query($conn,"SELECT productcode from products");
                              while ($get = mysqli_fetch_array($pc,MYSQLI_BOTH)) {
                                ?>
                                <option value="<?php echo $get['productcode'] ?>"><?php echo $get['productcode'] ?></option>
                              <?php }
                              ?>
                          </select></td>

                            <td><input type="text" id="1" name="brand[]" placeholder="Brand" class="form-control name_list" required  value=""></td>
                                                        <td><input type="text" id="1" value="" name="productdescription[]" placeholder="Product Desc" class="form-control name_list" required></td>
                                                        <td><input type="number" id="1" name ="qty[]" placeholder="Quantity" class="form-control name_list" required></td>
                            <td><input type="number" id="1" name ="cost[]" placeholder="Cost" class="form-control name_list" required></td>
                                                        <td><input type="number" id="1" name ="um[]" placeholder="UM" class="form-control name_list" required></td>
                                                        <td><input type="text" id="1" name="vendor[]" placeholder="Vendor" class="form-control name_list" required></td>
                                                        <td><button type="button"  name="add" id="add" class="btn btn-success">+</button></td>
                                                </tr>
                                        </table>

+
这是append的脚本

$(document).ready(function(){
                var i=1;
                $('#add').click(function(){
          <?php $drp = mysqli_query($conn,"SELECT * FROM products"); ?>
                        i++;
                        $('#dynamic_field').append('<tr id="row'+i+'">' +

                                '<td><select id="'+i+'" name="productcode[]" placeholder="Product Code" onchange="getState(this.value)" class="form-control name_list" required  value=""><?php $pc = mysqli_query($conn,"SELECT productcode from products");while ($get = mysqli_fetch_array($pc,MYSQLI_BOTH)) {?>  <option value="<?php echo $get['productcode'] ?>"><?php echo $get['productcode'] ?></option><?php }?> </select></td>' +
                '<td><input type="text" name="brand[]" placeholder="Brand" class="form-control name_list" required id="'+i+'"></td>' +
                                '<td><input id="'+i+'" type="text" name="productdescription[]" placeholder="Product Desc" class="form-control name_list" required></td>' +
                                '<td><input id="'+i+'" type="number" name="qty[]" placeholder="Quantity" class="form-control name_list" required></td>' +
                '<td><input id="'+i+'" type="number" id="costs" name ="cost[]" placeholder="Cost" class="form-control name_list" required></td>' +
                '<td><input type="number" id="'+i+'" name ="um[]" placeholder="UM" class="form-control name_list" required></td>' +
                                '<td><input id="'+i+'" type="text" name="vendor[]" placeholder="Vendor" class="form-control name_list" required></td>' +
                                '<td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">-</button></td></tr>');
                });
});
$(文档).ready(函数(){
var i=1;
$('#添加')。单击(函数(){
i++;
$(“#动态_字段”)。追加(“”+
'   ' +
'' +
'' +
'' +
'' +
'' +
'' +
'-');
});
});
这是用户界面

看起来您试图直接从
JS
使用
php
代码,但这行不通。看起来你还认为你在点击处理程序中迭代了一些东西,我这么说是因为你在使用
I
,但我不明白为什么这些代码会执行不止一次,在我所能看到的任何地方都没有
每个
调用或
for
循环。看起来你试图直接从你的
JS
使用
php
代码,这是行不通的。看起来你还认为你在点击处理程序中迭代了一些东西,我说,因为你使用的是
I
,但我不明白为什么这些代码会执行不止一次,我所能看到的任何地方都没有
每次调用或
for
循环。