2015-10-06 46 views
1

我有一個通過PHP生成的比薩菜單,其中包含一系列複選框。然後我想創建一個確認頁面,列出所選的項目。我嘗試使用for循環來創建披薩配料:PHP-用複選框填充表單,然後將選擇發送到確認頁

print("<h3>Toppings</h3>"); 
$toppings = array("Chicken", "Hamburger", "Peperoni", "Hot Sausage", "Extra Cheese", "Bell Peppers", "Jalape&ntilde;o Peppers", "Mushrooms", "Olives", "Onions"); 
for ($i=0; $i < $count($toppings); $i++){ 
    print ("<input type=\"checkbox\" name=\"choice[]\" value=$i> $toppings[$i]<br>"); 
} 

但是,然後形式永遠不會填充。所以我用這個代碼,而不是:

<!DOCTYPE html> 

<html> 
    <head> 
     <title>Pirate Pete's Pizza</title> 
    </head> 

    <body> 

     <?php 
     include("header.php"); 
     print("<h2>Choose the following:</h2>"); 

     //Start form 
     print("<form action=\"http://csci1450.spcompsci.org/~jsword/HW/HW3/confirmation.php\" method=\"POST\">"); 

     //Crust Selection - dropdown menu 
      print("<h3>Crust</h3>"); 
      $crust = array('Hand Tossed', 'Thick Crust', 'Deep Dish', 'New York Style', 'Stuffed Crust'); 
      print("<select name=\"crust\">"); 
      foreach ($crust as $item1){ 
        print ("<option>$item1</option>"); 
       } // end foreach crust 
      print("</select>"); 


     //Topping Selection - checkboxes 
      print("<h3>Toppings</h3>"); 
       $toppings = array("Chicken", "Hamburger", "Peperoni", "Hot Sausage", "Extra Cheese", 
        "Bell Peppers", "Jalape&ntilde;o Peppers", "Mushrooms", "Olives", "Onions"); 
       for ($i=0; $i < $count($toppings); $i++){ 
        print ("<input type=\"checkbox\" name=\"choice[]\" value=$i> $toppings[$i]<br>"); 
       } //end for loop - toppings   

      ?> 
     <br><input type="submit" value="Submit"> 
     <input type="reset" value="Erase and Reset" id="reset"> 
     </form> 
    </body> 

</html> 

現在我的菜單打印精細,但被髮送到我的確認頁的結果是「上」,因爲被選中的複選框重複多次的話。下面是確認頁面代碼:

<!DOCTYPE HTML> 
<html> 
    <head> 
     <title>Order Confirmation</title> 
    </head> 


    <body> 

     <?php 
     include("header.php"); 
     print("<h2>Order Confirmation</h2>"); 
     $crust = $_POST["crust"]; 
     $choice=$_POST["choice"]; 
     $toppings = array("Chicken", "Hamburger", "Peperoni", "Hot Sausage", "Extra Cheese", 
        "Bell Peppers", "Jalape&ntilde;o Peppers", "Mushrooms", "Olives", "Onions"); 

     print("You chose a $crust pizza with:<ul>"); 
     foreach($choice as $item){ 
      print("<li>$item</li>");  
     }//end foreach toppings 
     print("</ul>"); 
     ?> 
     <br><br><a href="http://csci1450.spcompsci.org/~jsword/HW/HW3/hw3.php"> 
     <input type ="button" value="Back to Menu"><a> 
    </body> 
</html> 

你也可以把它裝在服務器here上(假設我沒有在平均時間與它搞砸)

那麼,怎麼辦,我不是獲得foreach循環發送可用的信息,如字符串或索引#,或者獲取for循環來填充我的表單?

+0

你不需要'print()'每個html標籤 –

+0

我建議在你的'$ _POST'上使用'isset',否則如果用戶在沒有數據的情況下訪問那個頁面就會中斷。 – Script47

+0

@Burning晶體 - 即使它在PHP標籤內? – Gned

回答

0

我懷疑你的問題是在這條線:

print ("<input type=\"checkbox\" name=\"choice[]\" value=$i> $toppings[$i]<br>");

value=$i缺少引號。嘗試value=\"$i\"

編輯:也許我也建議你包的輸出在label標籤,以便更容易使用。這樣他們不必在複選框內單擊,他們也可以單擊頂部的名稱。像:

print ("<label><input type=\"checkbox\" name=\"choice[]\" value=$i> $toppings[$i]</label><br>");

+0

啊!這總是很愚蠢的簡單。謝謝! – Gned

0

這是你如何處理多個複選框。

<?php 
$output = "<h2>Order Confirmation</h2>"; 
if (array_key_exists('choice', $_POST)) { 
    //print_r($_POST['choice']); 
    //echo '<br>'; 
    $choice = serialize($_POST['choice']);// set post array to string value 
    $choice= unserialize($choice); 
    //print_r($choice); 
    //echo '<br>'; 
    foreach($choice as $item) { 
     // echo $item . '<br>'; 
     $output .= "<li>$item</li>"; 
    } 
    $output .= '</ul>'; 
} 

我包括註釋掉調試器輸出,你可以用它來使它做你想做的。

此外,如別人注意,有沒有需要打印或回聲每一個HTML行,只是將它們連接成一個變量,調用它,比如說,$輸出,然後在最後,這樣做:

echo $output; 

它更加整潔,而且在服務器上更容易,速度更快。每個打印/回聲基本上都會向請求的瀏覽器發送下一頁html頁面,而將它們全部放到一個變量中意味着您只輸出一次。正如我們都在編程課上學到的那樣,io是迄今爲止大多數編程中最昂貴的部分,所以將它保持在最低限度。它也導致更可讀的代碼。