2012-04-27 69 views
1

我正在開發一些PHP產品的訂購頁面。用戶需要能夠選擇多種類型的產品,因此在HTML表單中需要複選框。是否可以通過HTML表單(複選框)發佈多個「值」?

我已經通過「name」屬性爲複選框建立了一個數組。

我的問題是,我想顯示用戶在確認頁面上選擇的內容,所以我希望這些複選框的「值」最終返回產品名稱和產品價格。至於我可以告訴我要只有一個值被卡住,所以我試圖來解決這個問題:

<?php 
//variables for the array and the count 
$parray = $_POST['product']; 
$pcount = count($parray); 

//arrays for each product's information; i've only listed one for simplicity 
$br = array("Big Red", 575); 

/*The idea for this was for each product ordered to add a line giving the product information and display it. When the $key variable is assigned, it is stored simply as a string, and not an array (yielding [0] as the first letter of the string and [1] as the second), which I expected, but my general idea is to somehow use $key to reference the above product information array, so that it can be displayed here*/ 


if (!empty($parray)) 
{ 
    for ($i=0; $i < $pcount; $i++) 
{ 
    $key = $parray[i]; 
    echo "<tr><td height='40'></td><td>" . $key[0] . "</td><td>" . $key[1] . "</td></tr>"; 

} 
} 
?> 

反正讓我的$關鍵變量實際上充當如果它是數組的名稱設置爲? 如果沒有,有沒有什麼好的方法可以做到這一點?

+0

什麼是您的HTML是什麼樣子? – PeeHaa 2012-04-27 17:42:06

回答

5

所以在你的HTML表格,你需要使每個複選框是這樣的:

<input type="checkbox" name="selectedIDs[]" value="$key" /> 

其中$key是項目編號。

然後,在您的PHP中,您將有一個$_POST["selectedIDs"]變量,它是檢查的所有項目編號的數組。

假設你已經有了一個產品列表數組是這樣的:

for($i = 0; $i < count($_POST["selectedIDs"]); $i++) { 
    $key = $_POST["selectedIDs"][$i]; 
    $product = $products[$key]; 
    echo "<tr><td height='40'></td><td>" . $product[0] . "</td><td>" . $product[1] . "</td></tr>"; 
} 

唯一的區別:

$products = array(1 => array("Big Red", 575), 2 => array("Spearmint", 525)); 

然後,您可以使用這樣一個循環打印所選產品的列表在這和你寫的內容之間是我的$products數組是二維的,我的$key用於從$products數組中獲取相關產品。

3

您可以給的value s提供相應項目的ID值。然後,您將收到一組ID,然後您可以再次映射到$br陣列,您可以在其中查找ID的名稱和價格。你可以使用數組鍵ID,所以:

<!-- Form: --> 
<input type="checkbox" name="product[]" value="1" /> 
<input type="checkbox" name="product[]" value="..." /> 
<input type="checkbox" name="product[]" value="15" /> 
[...] 

<?php 
$product = array(); 
$product[1] = array('name' => "Big Red", 'price' => 575); 
$product[...] = array('name' => "...", 'price' => ...); 
$product[15] = array('name' => "Small Green", 'price' => 475); 

foreach ($_POST['product'] as $pId) 
{ 
    echo $products[$pId]['name'] . " costs " . $products[$pId]['price']; 
} 
?> 

當然如果你的陣列從數據庫起源,可以使用表的ID的作爲複選框值。然後,您可以使用逗號將$ _POST ['product'](當然是在轉義之後)爆炸,然後在SELECT ... WHERE ID IN ($productIds)查詢中使用它。

+0

謝謝你們倆。非常有幫助,我很感激。 – Esten 2012-05-04 16:14:50

1

如果我正確理解你,你必須複選框是這樣的:

<input type="checkbox" name="product_id[]" value="1">Product #1<br/> 
<input type="checkbox" name="product_id[]" value="2">Product #2<br/> 
<input type="checkbox" name="product_id[]" value="3">Product #3<br/> 

這應該張貼任何檢查爲陣列到服務器。

0

gen_paper1.php

<div style="overflow: auto; height: 500px; border: 1px solid;"> 
           <form action="" method="post"> 
           <fieldset style="font-size: 15px; font-weight: bolder;"> 
            <h4><u>Your Existing Header's are listed below</u> </h4> 
            <hr/> 

           <?php 
             $xhdr = mysql_query("SELECT * from s_user_header ORDER BY 'ASC'"); 
              while($rheader = mysql_fetch_array($xhdr)){ 
                $h = $rheader['h_content']; 
                $hid = $rheader['h_id'];?> 
            <script> 
             $(document).ready(function(){ 
              //$('input[type=checkbox]').val('<?php echo $hid ; ?>').tzCheckbox({labels:['<?php echo $h; ?>','<?php echo $h; ?>']}); 
              //$('#c').tzCheckbox({labels:['<?php echo $h; ?>','<?php echo $h; ?>']}); 
              $('').tzCheckbox({labels:['<?php echo $h; ?>','<?php echo $h; ?>']}); 
             }); 
            </script> 
            <table> 
              <td><input type="checkbox" id="c" name="u_hdr[]" value="<?php echo $hid; ?>"></td> 
              <td style="font-weight: bolder"><?php echo $h."<br />"; ?></td> 
            </table> 
            <?php } ?> 
           </fieldset> 
          </div> 

gen_paper2 ..... 3 ..... 我需要在進一步的頁面這一切的價值觀,所以採取了隱變量

<input type="hidden" name="hstdid" value="<?php echo $stdid; ?>"> 
         <input type="hidden" name="hsubid" value="<?php echo $subid; ?>"> 
         <input type="hidden" name="hdifid" value="<?php echo $difid; ?>"> 
         <input type="hidden" name="hmarks" value="<?php echo $t_marks; ?>"> 
         <input type="hidden" name="h_hid[]" value="<?php echo $hh; ?>"> 

gen_paper4.php

$getstdid = $_POST['hstdid3'] ; 
      $getsubid = $_POST['hsubid3'] ; 
      $getdifid = $_POST['hdifid3'] ; 
      $gethmarks = $_POST['htmarks'] ; 
      $hdr= $_POST['h_hdrid'] ; 
      $h = implode($hdr); 


<table> 
              <?php 
                $h1 = explode(",",$h); 
                count($h1) . "<br />"; 
                for($i=0;$i<count($h1);$i++){ 
                 $h1[$i]; 
                  $xheader = mysql_query("SELECT * from s_user_header WHERE h_id = ".$h1[$i]); 
                  while($row = mysql_fetch_array($xheader)){ ?> 
                   <tr> 
                   <td><b> 
                   <?php echo $i + 1 . ".&nbsp&nbsp&nbsp"; ?> 
                   </b></td><td> 
                   <?php echo $header = $row['h_content'] . "<br />"; 
                   ?></td> 
                   </tr> 
                   <?php 
                  } 
                } 
              //echo $header; ?> 
              </table> 
相關問題