-2

我有一个显示带有单选按钮的项目的网站。我想让 PHP 代码在用户提交代码时显示总数。

以下是我目前拥有的代码以及商店中每件商品的价格。

<?php 

$keybored = $_POST["keybored"];
$mouse = $_POST["mouse"];
$harddrive = $_POST["harddrive"];
$desktop = $_POST["desktop"];





$keyboredprice = 5.00;
$mouseprice =  3.50;
$hardriveprice = 20.00;
$desktopprice = 100;



$total = $prices;

echo "$total";


 ?>

我正在使用的 HTML 代码发布在下面,用于网络商店。

<!DOCTYPE html>
<html>
     <head>
        <style>
        body {background-color: lightblue;}
        h1   {color: gray;}
            h2 {color: white;
        
            border-size: 1;
            width: 200px;
  border: 15px solid black;
  padding: 50px;
  margin: 10px;}
            p  {color: white;
            font-size: 200%;}
            img {width:350px;height:300px;}
            
        </style>
    </head>
<body>
    <form action="purchase.php" method="post">
<h1> Welcome to computer parts store</h1>
<h2>Keybored</h1>
<img src="keybored.jpg">
<p> Select how many <br>
<input type="radio" name="keybored" value="1"">1<br>
<input type="radio" name="keybored" value="2">2<br>
<input type="radio" name="keybored" value="3"> 3<br> <br>
</p>

<h2>Mouse</h1>
<img src="mouse.jpg">
<p> Select how many <br>
<input type="radio" name="mouse" value="1"">1<br>
<input type="radio" name="mouse" value="2">2<br>
<input type="radio" name="mouse" value="3"> 3<br> <br>
</p>

<h2>HarDrive</h1>
<img src="harddrive.jpeg" >
<p> Select how many <br>
<input type="radio" name="harddrive" value="1">1<br>
<input type="radio" name="harddrive" value="2">2<br>
<input type="radio" name="harddrive" value="3"> 3<br> <br>
</p>

<h2>Desktop</h1>
<img src="Desktop.jpg">
<p> Select how many <br>
<input type="radio" name="desktop" value="1">1<br>
<input type="radio" name="desktop" value="2">2<br>
<input type="radio" name="desktop" value="3"> 3<br> <br>
</p>


<p>
   First Name: <input type="text" name="fname" ><br>
       Last Name: <input type="text" name="lname" ><br>
        Card type: <input type="text" name="type" > <br>
       Card Number: <input type="text" name="Card" > <br>
       Expiration date: <input type="text" name="exp" > <br>
        <input type="submit" value="Submit order"><br>
</p>
</form>
</body>
</html>

4

2 回答 2

0

在尝试向用户展示其价值之前,您需要计算价格。
$price = $keyboredprice + $mouseprice + $hardriveprice + $desktopprice; $total = $price echo "$total"

于 2020-04-15T22:42:49.640 回答
0

很简单:数量*价格。

<?php 

$keybored = $_POST["keybored"] ?? 0;
$mouse = $_POST["mouse"] ?? 0;
$harddrive = $_POST["harddrive"] ?? 0;
$desktop = $_POST["desktop"] ?? 0;

$keyboredprice = 5.00;
$mouseprice =  3.50;
$hardriveprice = 20.00;
$desktopprice = 100;

$total = ($keybored * $keyboredprice) + ($mouse * $mouseprice) + ($hardrive * $hardriveprice) + ($desktop * $desktopprice);

echo "$total";

PHP 是松散类型的,它将为您将表单数量字符串转换为整数,并且还将在乘法中保留浮点价格。

如果没有提交任何表单元素,这将使用 php7 空合并运算符 (??) 将默认数量设置为 0。

于 2020-04-15T22:56:44.370 回答