Reputation: 23
Help me get the value of total amount where: 1 column is displays the amount from db (I while looped it), multiply it by input tag ((no of copies). this is inside the while loop) then multiply them to get the total amount. How to pass the value of my input tag in php while loop?
Here is the image:
<?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>";
?>
Upvotes: 0
Views: 477
Reputation: 556
Maybe what you need is to use some javascript to update the total amount after user input the number of copy (onkeyup
event).
<?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>";
?>
The idea is first to set ID on the input
and td
for accessing via javascript.
Then, create a function to calculate the total.
Then, use document.getElementById
function to get and set the value.
Further reading HERE.
Hope this help.
Upvotes: 1
Reputation: 2104
You haven't given us the name of the column in the DB, of the name or the field in the form. Something like:
$inputVal=$_REQUEST['my_input_field'];
$sql = sprintf("SELECT SUM(my_col)*%d FROM account_db", $inputVal);
where you replace my_input_field and my_col with the real values should work.
Upvotes: 0