帮我获取总金额,其中: 第一列显示 db 的金额(我 while 循环它),将其乘以输入标签((副本数)。这是在 while 循环内),然后将它们相乘以获得总金额。如何在 php while 循环中传递输入标记的值?
这是图片:
<?php
$result = mysqli_query($link,"SELECT * FROM account_db");
$row = mysqli_fetch_row($result);
echo"<html>";
echo"<center>";
echo "<form method='POST'>";
echo "<table style='border:1px solid black' name='pleasework'>";
echo"<th>FILES</th>";
echo"<th>AMOUNT</th>";
echo"<th>NO. OF COPIES</th>";
echo"<th>TOTAL AMOUNT</th>";
$answer = $row[2] * noofcopies;
while($row)
{
echo"<tr>";
echo "<td id='transfile'>$row[1] </td>";
echo "<td align=center>$row[2] </td>";
echo "<td align=center><input type='number' name='noofcopies' onkeyup> </td>";
echo "<td align=center value=''>$answer</td>";
echo"</tr>";
}
echo "</table>";
echo " <input type='submit' value='Transact' name='transaction'>";
echo"</center>";
echo"</html>";
?>
最佳答案
也许您需要的是在用户输入副本数量后使用一些 JavaScript 来更新总金额(onkeyup
事件)。
<?php
$result = mysqli_query($link,"SELECT * FROM account_db");
$row = mysqli_fetch_row($result);
echo"<html>";
echo "<script>";
echo "function calculate(amount, id){ document.getElementById('total' + id).innerHTML = parseInt(document.getElementById('copy' + id).value) * amount; }";
echo "</script>";
echo"<center>";
echo "<form method='POST'>";
echo "<table style='border:1px solid black' name='pleasework'>";
echo"<th>FILES</th>";
echo"<th>AMOUNT</th>";
echo"<th>NO. OF COPIES</th>";
echo"<th>TOTAL AMOUNT</th>";
$counter = 0;
while($row)
{
echo"<tr>";
echo "<td id='transfile'>$row[1] </td>";
echo "<td align=center>$row[2] </td>";
echo "<td align=center><input type='number' id='copy" . $counter . "' name='noofcopies' onkeyup='calculate(" . $row[2] . ", " . $counter . ")'> </td>";
echo "<td align=center value='' id='total" . $counter . "'></td>";
echo"</tr>";
$counter++;
}
echo "</table>";
echo " <input type='submit' value='Transact' name='transaction'>";
echo"</center>";
echo"</html>";
?>
这个想法是首先在input
和td
上设置ID,以便通过javascript访问。
然后,创建一个函数来计算总数。
然后,使用 document.getElementById 函数获取和设置该值。
进一步阅读HERE .
希望这有帮助。
关于javascript - 传递 while 循环内部的输入类型标记的值,其中 while 循环位于 php 内部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46447937/