I am creating an order form for a project. I pulled data from the database with the Foreach loop to the table I created. But when I multiply the quantity and unit price data, the code works only for the data in the first row of the table. How can I revise this code for all incoming loop data?
Cart.php:
<form action="" method="POST">
<table class="table table-sm mb-3 text-center align-middle">
<thead>
<tr>
<th>Product Name</th>
<th>Quantity</th>
<th>Unit Price</th>
<th>Total Price</th>
</tr>
</thead>
<tbody>
<?php foreach($ProductTbl as $productdata){ ?>
<tr>
<td><?= $productdata->productname ?></td>
<td><input class="w-25 text-center quantity" type="text" name="quantity[]" value="0"></td>
<td><input class="w-25 text-center unitprice" type="text" name="unitprice[]" value="<?= $productdata->unitprice ?>" disabled="disabled"></td>
<td><input class="w-25 text-center totalprice" type="text" name="totalprice[]" value="" disabled="disabled"> €</td>
</tr>
<?php } ?>
</tbody>
</table>
Javascript code:
$(document).ready(function() {
$('.quantity').keyup(function() {
var quantity = $('.quantity').val();
var unitprice = $('.unitprice').val();
var totalprice = $('.totalprice').val();
var result = quantity * unitprice;
$('.totalprice').val(result);
});
});
});
Print: Image
How do I edit the code to run on all lines?