我试图用复选框表单提交多个数组,但我现在只能提交一个数组,这里是我到目前为止
在这个例子中,我使用delete []数组提交一个数组数组,这个数组得到正确的处理,我也想提交数组条件[]这个没有得到正确的处理,最好的方法是解决这个问题?
$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>
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
我怀疑的问题是,只有被勾选的复选框将被传回服务器,而所有隐藏的字段将始终被传递,这样数组的长度将会不同,并且密钥不会对应.
对此的解决方案实际上比较简单 – 您只需要指定条件数组的键,以便可以再次匹配值.这样的东西
HTML:
<tr> <td> <input type='hidden' name='condition[100001]' value='good' /> <input type='checkBox' name='delete[]' value='100001' /> </td> </tr> <tr> <td> <input type='hidden' name='condition[100002]' value='new' /> <input type='checkBox' name='delete[]' value='100002' /> </td> </tr>
PHP:
foreach ($_POST['delete'] as $delete) { $condition = $_POST['condition'][$delete]; // Do stuff }
这将$_POST [‘condition’]数组中的值与$_POST [‘delete’]数组备份起来,所以一切都会再次匹配.
编辑
上面创建密钥的方式并不好,回想起来,这样做是错误的.
为了演示正确的方法,让我们想象一下,我们有以下数组,不错,简单:
$books = array( 10001 => 'good',10002 => 'new',10003 => 'new',10004 => 'good' );
我们需要做的是捆绑与每本书相关联的两个输入,这意味着我们需要一组可以匹配的键/值对.这听起来像是一个阵列给我.但与上面的示例不同,这些键应该与数据无关 – 它们不需要任何意义,因为我们所需要的就是数据.
我们需要做的是明确指定每个单个键(没有堆栈式数组推),并使密钥与其相关的数据不可知.
我们做得到:
$i = 0; foreach ($books as $isbn => $condition) { echo " <tr> <td> <input type='hidden' name='condition[$i]' value='$condition' /> <input type='checkBox' name='delete[$i]' value='$isbn' /> </td> </tr> "; $i++; }
…然后,当提交表单时,我们可以这样做:
// We still base our code on $_POST['delete'] - because this is the array that // depends on the user input. This time,though,we'll look at the keys as well foreach ($_POST['delete'] as $key => $isbn) { $condition = $_POST['condition'][$key]; // Do stuff }