【发布时间】:2012-08-02 23:18:29
【问题描述】:
我正在尝试使用复选框表单提交多个数组,但目前我只能提交一个数组,这是我目前所拥有的
在这个例子中,我提交了一个带有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>";
}
}
}
我想要的只是基本上能够将两个数组传递给我的 php 脚本
电流输出
100001
100002
100003
期望的输出
100001 good
100002 new
100003 new
【问题讨论】:
-
我很困惑你在问什么。我在你的代码中没有看到
$_POST['condition'],所以我不确定你在问什么。 -
我不确定 [] 技巧是否也适用于隐藏字段..
-
@David 我试图让 ['condition'] 工作,例如
foreach($_POST['condition'] as $checkbox) echo $checkbox."<br>";但这不起作用 -
您需要更详细地解释您的问题,并改写它,因为阅读和完全理解您想要的内容可能会非常混乱......
-
@favoretti 你知道一种可行的方法