Script for adding a set of form fields on clicking an Add More button This is a script that I use to add a set of form fields on clicking an Add More button:

&lt;script&gt;
        $(document).ready(function() {

            var InputsWrapper = $("#InputsWrapper");
            var AddButton = $("#AddMoreFileBox");
            var x = InputsWrapper.length;
            var FieldCount = 1;
            $(AddButton).click(function(e)//on add input button click
            {

                FieldCount++;
                $(InputsWrapper).append('&lt;tr&gt;&lt;td&gt;&lt;input id="find" type="text" name="item[]"&gt;&lt;/td&gt;&lt;td&gt;&lt;input id="find" type="number" name="qty[]"&gt;&lt;/td&gt;&lt;td&gt;&lt;input id="find" type="number" name="price[]"&gt;&lt;/td&gt;&lt;td&gt;&lt;input id="find" type="number" name="discount[]"&gt;&lt;/td&gt;&lt;td&gt; &lt;?php $query = mysqli_query($con, "SELECT * FROM `tax_vat` "); echo '&lt;select name="vat" id="find" data-required="true"&gt;&lt;option value=""&gt;Select Vat&lt;/option&gt;"';while ($row = mysqli_fetch_array($query)) {echo '&lt;option value="' . $row['vat_rate'] . '"&gt;' . $row['vat_description'] . '&lt;/option&gt;&lt;/br&gt;';}echo '&lt;/select&gt;';?&gt;&lt;/td&gt;&lt;td&gt;&lt;input id="find" type="number" name="total[]" readonly &gt;&lt;/td&gt;&lt;/tr&gt;');
                x++;
                return false;
            });
        });

    &lt;/script&gt;


This is the form:

            &lt;table id="InputsWrapper" &gt;
                &lt;tr&gt;
                    &lt;span class="small"&gt;&lt;a href="#" id="AddMoreFileBox" class="btn btn-info"&gt;Add More Field&lt;/a&gt;&lt;/span&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                    &lt;td&gt;&lt;label for='item'&gt;Item:&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;label for='qty'&gt;Quantity:&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;label for='price'&gt;Price:&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;label for='discount'&gt;Discount %&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;label for='vat'&gt;VAT %:&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;label for='total'&gt;Final Price:&lt;/label&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                    &lt;td&gt;&lt;input class='item form-control' id="find" type='text'  name='item[]' required&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;input class='qty form-control' id="find" type='number' name='qty[]' required&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;input class='price form-control' id="find" type='number' name='price[]' required&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;input class='discount form-control' id="find" type='number'  name='discount[]' required&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;?php $query = mysqli_query($con, "SELECT * FROM `tax_vat` "); echo '&lt;select name="vat" id="find" data-required="true"&gt;&lt;option value=""&gt;Select Vat&lt;/option&gt;"';while ($row = mysqli_fetch_array($query)) {echo '&lt;option value="' . $row['vat_rate'] . '"&gt;' . $row['vat_description'] . '&lt;/option&gt;&lt;/br&gt;';}echo '&lt;/select&gt;';?&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;input class='total form-control' id="find" type='number' name='total[]' readonly required&gt;&lt;/td&gt;
                &lt;/tr&gt;
            &lt;/table&gt;
            Total : &lt;input id='totaldp' class="form-control" type='number' name='totaldp' readonly required&gt;
            &lt;input type="hidden" id="name_id" name="name_id" /&gt;


In the form, there is a dropdown box which fetch certain details from the database. The PHP code to fetch and display the dropdown box is coded in the form so that it is displayed on the initial load of the page, and then, the same code is coded in the append function the script so that the same set of input fields are displayed on clicking the Add More button.

The whole thing works perfectly fine. But the code looks terribly ugly. Is there a better way to do it?
Put the php code outside the form, run it once and create a variable with the options, something like:

$dropDownList = '&lt;select name="vat" id="find" data-required="true"&gt;&lt;option value=""&gt;Select Vat&lt;/option&gt;';

$query = mysqli_query($con, "SELECT * FROM `tax_vat` "); 
while ($row = mysqli_fetch_array($query)) 
{
$dropDownList .= '&lt;option value="' . $row['vat_rate'] . '"&gt;' . $row['vat_description'] . '&lt;/option&gt;';
}

$dropDownList .= '&lt;/select&gt;';


Now, in your form you just need to &lt;?php echo $dropDownList?&gt; where you want the drop down to appear.

For your javascript you could do something like this:

var dropOption = "&lt;?php echo $dropDownList?&gt;";


And modify the append() html data like this:

$(InputsWrapper).append('&lt;tr&gt;&lt;td&gt;&lt;input id="find" type="text" name="item[]"&gt;&lt;/td&gt;&lt;td&gt;&lt;input id="find" type="number" name="qty[]"&gt;&lt;/td&gt;&lt;td&gt;&lt;input id="find" type="number" name="price[]"&gt;&lt;/td&gt;&lt;td&gt;&lt;input id="find" type="number" name="discount[]"&gt;&lt;/td&gt;&lt;td&gt;'+dropOption+'&lt;/td&gt;&lt;td&gt;&lt;input id="find" type="number" name="total[]" readonly &gt;&lt;/td&gt;&lt;/tr&gt;');


Hope this helps.