I'm creating an order form where the quantity is where the user will input in the text field, whereas the price is already stored in the database.
I want the quantity of product to be times with the price of the product and diplay the result in the total field.
And then count all the total and display in the ORDER TOTAL field. But I'm stuck in the middle of it, and I'm not sure how to do it.
I was hoping someone could guide me in this. Here's my code:
<?php
//setting connection to the database
$connection = mysqli_connect("localhost", "root", "", "SCINFO");
//checking the connection
if(mysqli_connect_errno()){
echo "Connection Failed!";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Order -- SHOPPERS||COLLECTION</title>
</head>
<body>
<h2 align="left">Purchase/Order Items</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); >">
<table cellpadding="15">
<tr bgcolor="#996699">
<td>Product ID</td>
<td>Product Name</td>
<td>Quantity</td>
<td>Price (RM)</td>
<td>Total</td>
</tr>
<?php
//prints the data in table
$result = mysqli_query($connection, "SELECT * FROM PRODUCT");
// loop through results of database query, displaying them in the table
while($row = mysqli_fetch_array( $result)) {
// echo out the contents of each row
echo "<tr>";
echo '<td>' . $row['PRD_ID'] . '</td>'; //prints out the product ID
echo '<td>' . $row['PRD_NAME'] . '</td>'; //prints out the product name
echo '<td><input type="text" name="qty"> X</td>';
echo '<td>'.$row['PRD_PRICE'].'</td>';
echo '<td><input type="text" name="total" value="" disabled="disabled"></td>';
echo '</tr>';
}
?>
<tr>
<td colspan="5"><hr></td>
</tr>
<tr>
<td colspan="4" align="right">ORDER TOTAL:</td>
<td><input type="text" name="ototal" value="RM " disabled="disabled"></td>
</tr>
<tr>
<td colspan="4" align="right"><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</form>
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
if($_POST["submit"]){
$qty = $_POST['qty'];
$price = $_POST['prc'];
if(isset($_POST['submit'])){
$sum = $qty * $price;
}
}
}
?>
</body>
</html>