I have a form where I have to add table rows on button click.
Here is the table row which I have to add dynamically: (addProdToGroup.php)
<tr style="text-align: center;" id="products">
<td><?php $j ?></td>
<td><select class="form-control" name="code" id="productID" style="width: 429px;">
<?php
$sql = "SELECT * FROM `product`";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option id='".$row['code']."' value='".$row['code']."'>".$row['pname']."</option>";
}
} else {
echo "0 results";
}
?>
</select>
</td>
<td><input type="text" name="hsnNo" id="hsnNo" readonly></td>
<td><input type="text" name="qty" id="qty" readonly></td>
<td class="coljoin"><input type="number" format="2" name="amount"></td>
<td>
<span class="fa fa-trash"></span>
</td>
</tr>
This is the table in which I have to add the above-mentioned rows: (order.php)
<table id="productTable" class="table-c">
<tr>
<th class="text-center" style="width: 5%;">SR No.</th>
<th class="text-center" style="width: 45%">DESCRIPTION</th>
<th class="text-center" style="width: 10%">HSN/SAC</th>
<th class="text-center" style="width: 10%">QTY IN-HAND</th>
<th class="text-center" style="width: 10%">ENTER OUTWARD QTY</th>
<th class="text-center" style="width: 5%">Delete</th>
</tr>
<div class="dynamics">
</div>
</table>
On clicking the button, an AJAX query is being called for the PHP code. The ajax code is: (order.php)
<script>
$(document).ready(function(){
$('#addOrderProduct').click(function(){
var j = 1;
var dataString = "j="+j;
$.ajax({
url: "addProdToOrder.php",
type: "post",
data: dataString,
success: function(response){
$('.dynamics').html(response);
}
});
});
})
</script>
The output should be like this: Format for the rows
Also, I have a script to update the fields upon selecting the product from the drop-down. How to make that run for every row added into the form? Here is the code for updating fields: (order.php)
$('#productID').change(function(){
var code = $(this).val();
console.log(code);
var dataString = 'code='+code;
$.ajax({
url: "getProdDets.php",
type: "post",
data: dataString,
success: function(response){
var Vals = JSON.parse(response);
console.log(Vals);
$("input[name='hsnNo']").val(Vals.hsnNo);
$("input[name='qty']").val(Vals.qty);
console.log(Vals.hsnNo);
console.log(response);
}
});
});