2013-11-21 71 views
0

我正在嘗試構建一個列表,用戶可以選擇任何複選框,並且可以將該行的所有選擇值發送到下一頁。在PHP中從複選框獲取數組值

以下是我有:

第一頁:

<table border="0"> 
     <tr> 
<td><a href="#" onClick="select_all('area', '1');">Check All</a> | <a href="#" onClick="select_all('area', '0');">Uncheck All</a></td> 
<td>Item Name</td> 
<td>Item Description</td> 

     </tr> 
<?php 
if ($num_rows == 0) { 
      return "No Data Found"; 
}else{ 
    while ($row = dbFetchAssoc($result)) { 
     $item_id = $row['item_id']; 
     $item_name = $row['item_name']; 
     $item_desc = $row['item_desc']; 
     $item_qty = $row['item_qty']; 
     $item_upc = $row['item_upc']; 
     $item_price = $row['item_price']; 
     $vendor_id = $row['vendor_id']; 
?> 
     <tr> 
     <td> <input type="checkbox" name="area[]" value="<?=$item_id?>" /></td> 
     <td><input type="text" name="item_name[]" value="<?=$item_name?>"></td> 
     <td><input type="text" name="item_desc[]" value="<?=$item_desc?>"></td> 
     </tr> 
<?php 
} 
} 
?> 
     <tr><td colspan="3"><input type="submit"></td></tr> 
    </table> 
    </form> 

下一頁:

<table> 
    <tr><td colspan="3"><?php "Total Item(s) selected: "; echo count($_POST['area']); ?></td></tr> 
<?php 

if(!empty($_POST['area'])) { 
    foreach($_POST['area'] as $check) { 
      echo "<tr><td>".$check."</td><td>".$_POST['item_name']."</td><td>".$_POST['item_name']."</td></tr>"; 
    } 
} 
?> 
</table> 

我需要閱讀ITEM_NAME和ITEM_DESC價值爲好。我如何得到它?

回答

0

您需要的關鍵:

if(!empty($_POST['area'])) { 
    foreach($_POST['area'] as $key => $check) { 
      echo "<tr><td>".$check."</td><td>".$_POST['item_name'][$key]."</td><td>".$_POST['item_desc'][$key]."</td></tr>"; 
    } 
} 

正如馬克在他回答說,這可能會是更好的設置鍵在你的循環生成輸入。

<?php 
if ($num_rows == 0) { 
      return "No Data Found"; 
}else{ 
    $counter = 0; 
    while ($row = dbFetchAssoc($result)) { 
     $counter++; 
     $item_id = $row['item_id']; 
     $item_name = $row['item_name']; 
     $item_desc = $row['item_desc']; 
     $item_qty = $row['item_qty']; 
     $item_upc = $row['item_upc']; 
     $item_price = $row['item_price']; 
     $vendor_id = $row['vendor_id']; 
?> 
     <tr> 
     <td> <input type="checkbox" name="area[<?=$counter?>]" value="<?=$item_id?>" /></td> 
     <td><input type="text" name="item_name[<?=$counter?>]" value="<?=$item_name?>"></td> 
     <td><input type="text" name="item_desc[<?=$counter?>]" value="<?=$item_desc?>"></td> 
     </tr> 
<?php 
} 
} 
?> 
0
foreach($_POST['area'] as $key => $check) { 
    echo $check . $_POST['item_name'][$key]; 
} 

假設有一個確切的1:你的三個提交陣列之間一一對應。請記住,複選框是不是檢查做不是獲取與表單提交,所以這是最有可能不是真實的,這段代碼將不會像書面工作。