Hi I have this table inside a form and in this form Im also fetching data from a csv file to display product informations in a table. I want a javascript code that when a user inserts a quantity value of the product he wants to buy the total column will automatically compute the total price = Quantity x Price
<?php
//name this functions.php
function displayOrderList()
{
// I ommitted the code for reading CSV File
while (($column = fgetcsv("file.csv",1024)) !== false)
$price = $column[0]; //this is the price i got from the CSV File
echo "
<tr>
<td><input type=text id=qty value=0></td>
<td><input type=text disabled id=price value=$price></td>
<td><input type=text disabled id=total value=0 ></td>
</tr>
}
//this is another php file to display the table
<?php
include ('functions.php');
?>
<html>
<head>
<title>Order Form</title>
</head>
<body>
<!--start of form-->
<form>
<table>
<tr>
<th>Quantity</th>
<th>Price</th>
<th>Total</th>
</tr>
<?php
displayProductList();
?>
</table>
<input type=submit value=Order>
</form>
</body>
</html>
There are a lot of missing codes here but I only want to focus on the dynamic table.
I want to know how will I get the values of the Quantity,Price,Total so I can compute the Total and automatically update the Total value using javascript.
Here's a script I have but it only computes the first element.
window.onkeyup=function()
{
var qty = document.getElementById('qty');
var price = document.getElementById('price');
var total = document.getElementById('total');
if (isNaN(qty.value))
{
qty.value=qty.value.replace(/\D/g,'');
alert('Only Numbers Allowed');
}
if (qty.value.length>0&&price.value.length>0)
{
total.value = parseInt(qty.value) * parseInt(price.value);
}
}