我想有一個複選框表單提交多個陣列,但我只能在此刻提交一個數組,這裏是我迄今爲止如何提交多個陣列複選框HTML表單
在這個例子中我正在提交一組數字與delete[]
數組,這個數組得到處理正確,我也想提交數組condition[]
這不會得到正確處理,什麼是解決此問題的最佳方法?
PHP代碼
$catalog = $database->getInventory();
if($catalog){
$numRows = sizeof($catalog);//count
echo "<b>Book Count:</b> ".$numRows."<br>";
echo "<form method='post' action='inventory.php'>";
echo "<table id='example' class='tablesorter' border='0' cellpadding='0' cellspacing='1'>";
echo "
<thead>
<tr>
<th>ISBN</th>
<th>Title </th>
<th>Rank </th>
<th>Condition </th>
<th><input type='checkbox' name='delete' value='all' /></th>
</tr>
</thead>\n";
foreach($catalog as $elem){
echo "
<tr>
<td>".$elem["isbn"]."</td>
<td>".$elem["title"]."</td>
<td>".$elem["rank"]."</td>
<td>".$elem["condition"]."</td>
<td>
<input type='checkbox' name='add[]'
value='".$elem['isbn']."_".$elem['condition']."_"."' />
</td>
</tr>";
}
echo "</table>";
echo "</form>";
}
例如HTML標記
<form method='post' action='inventory.php'>
<table>
<tr>
<td>
<input type='hidden' name='addInventoryBook' value='1'>
<input type='submit' value='Add' />
</td>
</tr>
<tr>
<td>
<input type='checkbox' name='add[]' value='100001_used' />
</td>
</tr>
<tr>
<td>
<input type='checkbox' name='add[]' value='100001_new' />
</td>
</tr>
<tr>
<td>
<input type='checkbox' name='add[]' value='100003_new' />
</td>
</tr>
</table>
</form>
PHP函數
function Inventory(){
if(isset($_POST['addInventoryBook'])){
if(isset($_POST['add']) && is_array($_POST['add'])){
$arr = array();
foreach($_POST['add'] as $checkbox){
$temp = explode("_", $checkbox);
$arr[] = array(
"isbn" => $temp[0],
"condition" => $temp[1],
"sub_condition" => $temp[2]
);
}
$this->addInventoryBook($arr);
}
else{
echo "No values have been set";
}
}
function addInventoryBook($arr){
foreach($arr as $elem){
//if used get sub-category
if($elem['condition']=='used'){
echo $elem['isbn']."-".ucfirst($elem['condition'])
.ucfirst($elem['sub_condition'])."<br>";
}
else if($elem['condition']=='new'){
echo $elem['isbn']."-".ucfirst($elem['condition'])."<br>";
}
}
}
所有我想要的我s到基本能夠兩個數組傳遞給我的PHP腳本
電流輸出
100001
100002
100003
所需的輸出
100001 good
100002 new
100003 new
我很困惑你在問什麼。我在代碼中的任何地方都看不到'$ _POST ['condition'],所以我不確定你在問什麼。 – David 2012-08-05 22:34:09
我不確定[]技巧是否適用於隱藏字段.. – favoretti 2012-08-05 22:35:13
@大衛即時嘗試讓['條件']工作,例如, '的foreach($ _ POST [「條件」]爲$複選框)回聲$勾選「
」;'但不起作用 – 2012-08-05 22:36:18