如何使用html表单提交多个数组复选框 [英] How to submit multiple array checkbox with html forms

查看:665
本文介绍了如何使用html表单提交多个数组复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用复选框的形式提交多个数组,但我现在只能提交一个数组,这是我迄今为止的内容



在这个例子我用 delete [] 数组提交数组数组,这个数组得到正确处理,我也想提交数组 condition [ ] 这个问题得不到解决,解决这个问题的最好方法是什么?

/ p>

  $ 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>
< th>标题& nbsp;& nbsp <><>>
" Rank& nbsp;& nbsp;< th>
条件& nbsp;& nbsp;< th>
< th><输入类型='复选框'名称='删除'值='全部'/>< / th>
< / tr>
< ; / THEAD> \\\
;


foreach($ catalog为$ elem){
echo
< tr>
< td>。$ elem [isbn ]。< / td>
< td>。$ elem [title]。< / td>
< td>。$ elem [rank] 。< / td>
< td>。$ elem [condition]。< / td>
< td>
< input type ='复选框'name''add']'
value ='。$ elem ['isbn']。_。$ elem ['condition']。_。'/>
< / td>
< / tr>;
}

echo< / table>;
回声< / 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函数

 函数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没有设定数值;



$ b函数addInventoryBook($ arr){
foreach($ arr as $ elem){
//如果已使用如果($ elem ['condition'] =='used'){
echo $ elem ['isbn']。 - 。ucfirst($ elem ['condition'])获得子类别

.ucfirst($ elem ['sub_condition'])。< br>;
}

else if($ elem ['condition'] =='new'){
echo $ elem ['isbn']。 - 。ucfirst($ ELEM [ '状态']) <峰; br> 中。
}

}
}

所有我想要的是基本上能够将两个数组传递给我的PHP脚本



当前输出

  100001 
100002
100003



<

  100001良好
100002新
100003新


解决方案

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



对此的解决方案实际上相对简单 - 您只需为条件数组指定键,以便可以再次将值重新匹配。像这样:

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];
//做东西
}

这将 $ _ POST ['condition'] 数组将备份到 $ _ POST ['delete'] 数组中,所有内容都会重新匹配。

编辑



上面创建键的方式并不好回想起来,这是做错的方式。



为了演示 right 的方式,让我们想象下面的数组,很好很简单:

  $ books = array(
10001 =>'good',
10002 =>'new',
10003 =>'new',
10004 =>'good'
);

我们需要做的是捆绑与每本书相关的两个输入,这意味着我们需要一组可匹配的键/值对。这听起来像一个阵列给我。但与上面的例子不同,密钥与数据无关 - 它们不需要任何意义,因为我们只需要数据。



我们需要什么要做的事情是明确指定每一个键(没有堆栈式阵列推送)并且使得键不可知它们相关的数据。



我们可以这样做: p>

  $ i = 0; 
foreach($ books as $ isbn => $ condition){
echo
< tr>
< td>
< input type =隐藏'name ='condition [$ i]'value ='$ condition'/>
< input type ='checkbox'name ='delete [$ i]'value ='$ isbn'/>
< / td>
< / tr>
;
$ i ++;
}

...然后,当表单提交时,我们可以做到这一点:

  //我们的代码仍然基于$ _POST ['delete']  - 因为这是数组
//取决于用户输入。然而,这一次,我们将查看关键字以及
foreach($ _POST ['delete'] as $ key => $ isbn){
$ condition = $ _POST ['condition' ] [$关键]
//做东西
}


I am trying to submit multiple arrays with a checkbox form but I am only able to submit one array at the moment, here is what I have so far

In this example I am submitting an array of numbers with the delete[] array, this array gets processed properly, I also want to submit the array condition[] this does not get processed properly, what is the best way to solve this issue?

php code

$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>";
}

example html markup

<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

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>";
      }

   }
}

All I want is to basically be able to pass two arrays to my php script

current output

100001
100002
100003

desired output

100001   good
100002   new
100003   new

解决方案

The problem that you are having, I suspect, is that only the checkboxes that are checked will be passed back to the server, whereas all the hidden fields will always be passed so the lengths of the arrays will differ and the keys wont correspond.

The solution to this is actually relatively simple - you just need to specify the keys for the condition array so you can match the values up again. Something like this:

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
}

This ties the values in the $_POST['condition'] array back up with the $_POST['delete'] array so everything will match up again.

EDIT

The way the keys are being created above is not great and in retrospect it is the wrong way to do it.

To demonstrate the right way to do it, let's imagine we have the following array, nice and simple:

$books = array(
  10001 => 'good',
  10002 => 'new',
  10003 => 'new',
  10004 => 'good'
);

What we need to do is tie up the two inputs that are associated with each book, which means we need a set of key/value pairs that can be matched up. That sounds like an array to me. But unlike the example above, the keys should be irrelevant to the data - they don't need to mean anything, because all we want is the data.

What we need to do is specify every single key explicitly (no stack-style array pushes) and make the keys agnostic of the data they relate to.

We can do this:

$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++;
}

...and then, when the form is submitted, we can do this:

// 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
}

这篇关于如何使用html表单提交多个数组复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆