I’m working on a small HTML form integrated with a table. The table has an input field named “name” which have default value from some variable $name as a default value with other two input field (city and country) without any default value. This works well for the first row. However, when I dynamically add more rows, the new input fields don’t display the default value $name in new dynamic created row field. Here’s my current code setup:
<html>
<body>
<table class="table table-bordered">
<thead class="table-success" style="background-color: #3fbbc0;">
<tr>
<th width="15%"><center>Name</th>
<th width="15%"><center>city</th>
<th width="15%"><center>country</th>
<th width="5%"></th>
<button type="button" class="btn btn-sm btn-success" onclick="BtnAdd()">Add Item</button>
</th>
</tr>
</thead>
<tbody id="TBody">
<tr id="TRow" class="d-none">
<td>
<input type="text" name="name[]" id="name" value = "<?php echo $name; ?>">
</td>
<td>
<input type="text" name="city[]" id="city" >
</td>
<td>
<input type="text" name="country[]" id="country" >
</td>
<td class="NoPrint">
<button type="button" onclick="BtnDel(this)">x</button>
</td>
</tr>
</tbody>
</table>
Script to add deynamic rows in table
<script type="text/javascript">
function GetPrint()
{
/*For Print*/
window.print();
}
function BtnAdd()
{
/*Add Button*/
var v = $("#TRow").clone().appendTo("#TBody") ;
$(v).find("input").val('');
$(v).find("input").autocomplete({
source: 'backend-script.php'
});
$(v).removeClass("d-none");
$(v).find("th").first().html($('#TBody tr').length - 1);
}
function BtnDel(v)
{
/*Delete Button*/
$(v).parent().parent().remove();
GetTotal();
$("#TBody").find("tr").each(
function(index) {
$(this).find("th").first().html(index);
}
);
}
</script>