在文本字段订单表单中打印总计

时间:2015-09-17 16:40:42

标签: php mysql

我正在创建一个订单表单,其中数量是用户在文本字段中输入的位置,而价格已存储在数据库中。

我希望产品数量与产品价格一致,并将结果显示在总字段中。

然后计算所有总数并显示在ORDER TOTAL字段中。但我陷入其中,我不知道该怎么做。

我希望有人可以指导我。这是我的代码:

<?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>

1 个答案:

答案 0 :(得分:0)

如果您说价格已经在数据库中,那么您为什么要使用$_POST['prc']?因为据我所知,当您想从表单中获取价值时,会使用$_POST[]。让我们假设

 <html>
    <body>
      <form method="post" >
          <input type="text" name="userName" value="" />
          <input type="submit" value="Click me"
      </form>          


<?php
       if ($_SERVER["REQUEST_METHOD"] == "POST") {
// POST is used to collect value of input field
         $name = $_POST['userName'];
         if (empty($name)) 
          {
           echo "Name is empty";
          } else {
           echo $name;
          }    
         ?>

    </body>
    </html>