2017-02-25 49 views
0

所以我正在做一個任務,我必須做一個比薩訂單。我是PHP新手,並且努力讓我的數學工作。 所以這是我的HTML用php添加多個變量

<!doctype html> 
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE"> 
<meta http-equiv="expires" content="0" /> 
<html lang="en"> 
<head> 
    <title> HW05 </title> 
    <link rel="stylesheet" href="hw05.css"> 
</head> 
<body> 
<form method="post" action="hw05.php"> 
<header> 
    <h1 id="title"> 
     Pizza Order 
    </h1> 
</header> 
<section id="ci"> 
    <h2 id="ciheader"> 
     Customer Information 
    </h2> 
    <p id="text"> 
     Name: <input type="text" name="name" size="15" /> <br /><br /> 
     Phone: <input type="text" name="number" size="15" /> <br /> <br /> 
      <input type="submit" value="Order" /> 
    </p> 
</section> 

<section id="size"> 
    <h2 id="sizeheader"> 
     Pizze Size 
    </h2> 
    <p id="radio"> 
     <input type="radio" name="size1" value="small"> Small <br /> 
     <input type="radio" name="size2" value="medium"> Medium <br /> 
     <input type="radio" name="size3" value="large"> Large 
    </p> 
</section> 

<section id="top"> 
    <h2 id="topheader"> 
     Toppings 
    </h2> 
    <p id="checkbox"> 
     <input type="checkbox" name="checkbox1" value="box"> Pepperoni <br /> 
     <input type="checkbox" name="checkbox2" value="box"> Sausage <br /> 
     <input type="checkbox" name="checkbox3" value="box"> Mushroom <br /> 
     <input type="checkbox" name="checkbox4" value="box"> Ham <br /> 
     <input type="checkbox" name="checkbox5" value="box"> Olives <br /> 
     <input type="checkbox" name="checkbox6" value="box"> Onions 
    </p> 
</section> 

<section id="price"> 
    <h2 id="priceheader"> 
     Topping Price 
    </h2> 
    <p id="dollars"> 
     $1.00 <br /> 
     $1.50 <br /> 
     $0.80 <br /> 
     $1.25 <br /> 
     $0.75 <br /> 
     $0.50 
    </p> 
</section> 
</form> 
</body> 
</html> 

於是從HTML我需要創建一個計算配料的數量,並增加了尺寸的總成本和配料一起PHP頁面。我無法弄清楚如何讓PHP將每種打頂的成本和比薩尺寸的成本加在一起。儘管我能夠讓PHP來計算澆頭。任何幫助將非常感謝。

+0

你應該向我們展示了'hw05.php'也 –

回答

1

你可以這樣做:

<input type="checkbox" name="topping[]" value="pepperoni"> Pepperoni <br /> 
<input type="checkbox" name="topping[]" value="sausage"> Sausage <br /> 
<input type="checkbox" name="topping[]" value="mushroom"> Mushroom <br /> 
<input type="checkbox" name="topping[]" value="ham"> Ham <br /> 
<input type="checkbox" name="topping[]" value="olives"> Olives <br /> 
<input type="checkbox" name="topping[]" value="onions"> Onions 

在你hw05.php文件:

$toppingToPriceMap = [ 
    "pepperoni" => 1, 
    "sausage" => 0.8, 
    "mushroom" => 1.25, 
    "ham" => 0.75, 
    "olives" => 0.5, 
    "onions" => 0.75 
]; 
if (isset($_POST["topping"]) { 
    $numberOfToppings = count($_POST["topping"]); 
    $cost = 0; 
    foreach ($_POST["topping"] as $topping) { 
     if (isset($toppingToPriceMap[$topping])) { 
      $cost += $toppingToPriceMap[$topping]; 
     } 
    }  
} 

echo "Selected $numberOfToppings toppings at a cost of $".$cost; 

注意:這僅僅是你的代碼需要修復的一個。

0

取決於您如何獲得Topping Price的價值,但是您可以嘗試使用$爲每個數字創建變量並添加它們。

+0

0

html角度來看,你應該考慮做以下修改:

<p id="radio"> 
    <input type="radio" name="size" value="small"> Small <br /> 
    <input type="radio" name="size" value="medium"> Medium <br /> 
    <input type="radio" name="size" value="large"> Large 
</p> 

注意,name屬性是大小爲所有3個選項,使用戶可以選擇的3

對於一個複選框,你會想做類似的事情,唯一的區別是對於那個你會想要將選項存儲在一個數組中。

<p id="checkbox"> 
    <input type="checkbox" name="checkbox1" value="1"> Pepperoni <br /> 
    <input type="checkbox" name="checkbox[]" value="2"> Sausage <br /> 
    <input type="checkbox" name="checkbox[]" value="3"> Mushroom <br /> 
    <input type="checkbox" name="checkbox[]" value="4"> Ham <br /> 
    <input type="checkbox" name="checkbox[]" value="5"> Olives <br /> 
    <input type="checkbox" name="checkbox[]" value="6"> Onions 
    </p> 

其餘的應該發生在您沒有爲我們提供的hw05.php

0

代碼很簡單。您需要檢查是否存在變量,然後檢查值。因此,像是..

$addonCost =0; 
if (isset($_POST['checkbox1'])) { 
    if ($_POST['checkbox1'] == "box") { 
     $addonCost += 1; // adding $1.00 
    } 
}