处理来自不同对象的多个$ _POST [英] dealing with multiple $_POST coming from different objects

查看:61
本文介绍了处理来自不同对象的多个$ _POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的表格,里面有很多复选框

I have a form like this, with lots of checkboxes

<form action="myscript.php" method="post">
   <select name="myselect>
      <option value="1">option1</option>
      <option value="2">option2</option>
      <option value="3">option3</option>
      ...
   </select>
   <ul>
      <li><input type="checkbox" name="aaa"/>something1</li>
      <li><input type="checkbox" name="aab"/>something2</li>
      <li><input type="checkbox" name="aac"/>something3</li>
      <li><input type="checkbox" name="aad"/>something4</li>
      ...
  </ul>
  <input type="submit" value="Submit"/>
</form>

(未定义选择选项的数量和复选框的数量,并且可能会有所不同) 我无法弄清楚如何在不将"select"值与"selecting"值混合"的情况下检索所有复选框$ _POST值...

(number of select's options and number of checkboxes are not defined and can vary) I can't figure out how to retrieve all the checkboxes $_POST values without "mixing" with my select value...

在类似的情况下,我只有很多文本框,我这样做:

In a similar case, in which i had only lots of textboxes i did this:

foreach($_POST as $key => $value) {
   ...
}

但是在这种情况下,我只有文本框...

but in this case i had ONLY textboxes...

推荐答案

除非具有名称,否则复选框不会提交任何数据.如果给它们全都相同的名称,那么它们将由任何理智的表单数据解析器分组.对于PHP(在设计的特定部分为边缘情况进行了过度设计)时,名称必须以[]结尾.

Checkboxes won't submit any data unless they have a name. If you give them all the same name, then they will be grouped by any sane form data parser. In the case of PHP (which is overengineered for edge cases in that particular part of its design) the name has to end with [].

那么您可以:

  <li><input type="checkbox" name="foo[]" value="aaa"/></li>
  <li><input type="checkbox" name="foo[]" value="aab"/></li>
  <li><input type="checkbox" name="foo[]" value="aac"/></li>
  <li><input type="checkbox" name="foo[]" value="aad"/></li>

并且:

foreach($_POST['foo'] as $value) {
   ...
}

将遍历所有选中复选框的所有值(未选中的复选框将不会成功,因此不会出现在数据中).

will loop over all the values of all the checked checkboxes (unchecked checkboxes will not be successful so won't be in the data).

这篇关于处理来自不同对象的多个$ _POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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