dtsnx44260 2015-09-17 16:40
浏览 20

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

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>
  • 写回答

1条回答 默认 最新

  • dongsu1539 2015-09-17 16:59
    关注

    if you are saying that the price is already in database then why are you using $_POST['prc'] ? Because till best of my knowledge the $_POST[] is used when you want to get value from form. let suppose

     <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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么