19

我正在尝试使用复选框表单提交多个数组,但目前我只能提交一个数组,这是我目前所拥有的

在这个例子中,我提交了一个带有数组的数字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&nbsp;&nbsp;&nbsp;</th>
           <th>Rank&nbsp;&nbsp;</th>
           <th>Condition&nbsp;&nbsp;</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
4

3 回答 3

36

我怀疑您遇到的问题是,只有选中的复选框才会被传递回服务器,而所有隐藏字段都将始终被传递,因此数组的长度会有所不同,并且键不会对应。

对此的解决方案实际上相对简单——您只需要指定condition数组的键,这样您就可以再次匹配这些值。像这样的东西:

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
}
于 2012-08-05T22:45:16.910 回答
3

I'm a little confused about what you are asking, but I think I can simplify this for you. I don't know how you are generating the values for the hidden fields, are they hard coded? Regardless, this system would work much better if it were simplified.

Try this out....

This will combine the info for the numbers and condition, then it will split them on the backend for handling. This way the information is passed at the same time.

<tr>
         <td>
            <input type='checkbox' name='delete[]' value='100001-good' />
         </td>
      </tr>

      <tr>
         <td>
            <input type='checkbox' name='delete[]' value='100002-new' />
         </td>
      </tr>

      <tr>
         <td>
            <input type='checkbox' name='delete[]' value='100003-new' />
         </td>
      </tr>

    <?php


       if(isset($_POST['deleteInventoryBook'])){
          if(isset($_POST['delete']) && is_array($_POST['delete'])){
             foreach($_POST['delete'] as $checkbox){
               $checkbox = explode('-', $checkbox);
               echo $checkbox[1];
               echo '<br />';
               echo $checkbox[0];
               echo '<br />';
             }
          }else{
             echo "No values have been set";
          }
       }


    ?>

Again, I don't know if this is helpful or not because I was a little misunderstood about what exactly you were trying to achieve, but I hope it was helpful.

于 2012-08-05T22:54:25.763 回答
1

您将不得不找到一种创造性的方法将多个隐藏字段作为数组传递给 PHP 处理程序,或者更改收集数据的方式。“序列化”数组似乎是最好的选择。

这个 StackOverflow 答案确实概述了您可以做什么,并且仍然应该与您的脚本的行为相匹配。祝你好运!

于 2012-08-05T22:38:57.863 回答