รายละเอียดของการตอบ ::
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script language="javascript">
function cal_am(qty,_num){
var amount = parseInt(qty) * parseInt(document.getElementById("price"+_num).value);
document.getElementById("amt"+_num).value= amount.toFixed(2);
var _sum=0;
for(var i=1;i<=3;i++){
_sum += parseInt(document.getElementById("amt"+i).value);
}
document.getElementById("sum_amt").value= _sum.toFixed(2);
}
</script>
</head>
<body>
<?php
$cnt = 3 ; // จำนวนแถวทั้งหมดที่ดึงขึ้นมา
$price[1] = 10 ;
$price[2] = 20 ;
$price[3] = 30 ;
$qty[1] = 10 ;
$qty[2] = 10 ;
$qty[3] = 10 ;
$amt[1] = $qty[1] * $price[1];
$amt[2] = $qty[2] * $price[2];
$amt[3] = $qty[3] * $price[3];
$sum_amt = $amt[1]+$amt[2]+$amt[3] ; ?>
<form action="" method="post" name="frmmain">
<table border="1">
<tr>
<td><div align="center">ราคา/หน่วย</div></td>
<td><div align="center">จำนวน</div></td>
<td><div align="center">ราคารวม</div></td>
</tr>
<?php for($i=1; $i<=$cnt; $i++){ ?>
<tr>
<td><div align="center"><input name="price<?php echo $i; ?>" id="price<?php echo $i; ?>" type="text" value="<?php echo $price[$i]; ?>"></div></td>
<td><div align="center"><input name="qty<?php echo $i; ?>" id="qty<?php echo $i; ?>" type="text" value="<?php echo $qty[$i]; ?>" onKeyUp="cal_am(this.value,<?=$i;?>);"></div></td>
<td><div align="center"><input name="amt<?php echo $i; ?>" id="amt<?php echo $i; ?>" type="text" value="<?php echo $amt[$i]; ?>"></div></td>
</tr>
<?php }
?>
<tr>
<td></td>
<td><div align="center">ยอดสุทธิ</div></td>
<td><div align="center"><input name="sum_amt" id="sum_amt" type="text" value="<?php echo $sum_amt; ?>"></div></td>
</tr>
</table>
</form>
</body>
</html>