As an example I have four input text boxes in a form. I am trying to calculate the cost of each item type (quantity * cost) and the compute a total of all items. I have placed the formula inside a while loop to calculate cost of all the possible items found in the form. I am not receiving any result in return for the total cost? Demo
php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$discount = $_POST['discount'];
$i = 1;
while(isset($_POST['quantity'.$i])){
$quantity = $_POST['quantity'.$i];
$cost = $_POST['cost'.$i];
$total = $quantity * $cost;
$i++;
}
}
html
<div id="contact-area">
<form action="" method="post">
<label>Quantity:</label> <input type="text" name="quantity1" id="quantity1"> <label>Cost:</label><input type="text" name="cost1" id="cost1"><br /><br />
<label>Quantity:</label> <input type="text" name="quantity2" id="quantity2"> <label>Cost:</label><input type="text" name="cost2" id="cost2"><br /><br />
<br /><br />
<input type="submit" value="Submit" >
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
echo"<p>Amount Due: $". $total ."</p>";
}
?>
</div>