首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从复选框中生成数组?

如何从复选框中生成数组?
EN

Stack Overflow用户
提问于 2019-01-21 16:56:02
回答 2查看 25关注 0票数 0

因此,我想从我的所有复选框中创建一个数组,然后检查它们是否被选中。目前,我正在尝试以下方法,这是不起作用的:

代码语言:javascript
复制
<?php
  $napok[] = $_POST['napok[]'];

  isset($_POST['napok[0]'])
?>



<form class="asd" id="checkbox" action="index.php"  method="post">
    <table>
      <thead>
          <tr>
            <th></th>
              <th>1.</th>
              <th>2.</th>
              <th>3.</th>
              <th>4.</th>
              <th>5.</th>
              <th>6.</th>
          </tr>
        </thead>

        <tbody>
            <tr>
              <th>Hétfő</th>
                <td><input type="checkbox" id="he1" onclick="checkingFunction(this)" name="napok[]"><label id="he1" for="he1">40</label></td>
                <td><input type="checkbox" id="he2" onclick="checkingFunction(this)" name="napok[]"><label id="he2" for="he2">40</label></td>
                <td><input type="checkbox" id="he3" onclick="checkingFunction(this)" name="napok[]"><label id="he3" for="he3">40</label></td>
                <td><input type="checkbox" id="he4" onclick="checkingFunction(this)" name="napok[]"><label id="he4" for="he4">40</label></td>
                <td><input type="checkbox" id="he5" onclick="checkingFunction(this)" name="napok[]"><label id="he5" for="he5">40</label></td>
                <td><input type="checkbox" id="he6" onclick="checkingFunction(this)" name="napok[]"><label id="he6" for="he6">40</label></td>
            </tr>
            <tr>
              <th>Kedd</th>
              <td><input type="checkbox" id="ke1" onclick="checkingFunction(this)" name="napok[]"><label id="ke1" for="ke1">40</label></td>
              <td><input type="checkbox" id="ke2" onclick="checkingFunction(this)" name="napok[]"><label id="ke2" for="ke2">40</label></td>
              <td><input type="checkbox" id="ke3" onclick="checkingFunction(this)" name="napok[]"><label id="ke3" for="ke3">40</label></td>
              <td><input type="checkbox" id="ke4" onclick="checkingFunction(this)" name="napok[]"><label id="ke4" for="ke4">40</label></td>
              <td><input type="checkbox" id="ke5" onclick="checkingFunction(this)" name="napok[]"><label id="ke5" for="ke5">40</label></td>
              <td><input type="checkbox" id="ke6" onclick="checkingFunction(this)" name="napok[]"><label id="ke6" for="ke6">40</label></td>
            </tr>
            <tr>
              <th>Szerda</th>
              <td><input type="checkbox" id="sze1" onclick="checkingFunction(this)" name="napok[]"><label id="sze1" for="sze1">40</label></td>
              <td><input type="checkbox" id="sze2" onclick="checkingFunction(this)" name="napok[]"><label id="sze2" for="sze2">40</label></td>
              <td><input type="checkbox" id="sze3" onclick="checkingFunction(this)" name="napok[]"><label id="sze3" for="sze3">40</label></td>
              <td><input type="checkbox" id="sze4" onclick="checkingFunction(this)" name="napok[]"><label id="sze4" for="sze4">40</label></td>
              <td><input type="checkbox" id="sze5" onclick="checkingFunction(this)" name="napok[]"><label id="sze5" for="sze5">40</label></td>
              <td><input type="checkbox" id="sze6" onclick="checkingFunction(this)" name="napok[]"><label id="sze6" for="sze6">40</label></td>
            <tr>
              <hr>
              <th>Csütörtök</th>
              <td><input type="checkbox" id="csu1" onclick="checkingFunction(this)" name="napok[]"><label id="csu1" for="csu1">40</label></td>
              <td><input type="checkbox" id="csu1" onclick="checkingFunction(this)" name="napok[]"><label id="csu2" for="csu2">40</label></td>
              <td><input type="checkbox" id="csu1" onclick="checkingFunction(this)" name="napok[]"><label id="csu3" for="csu3">40</label></td>
              <td><input type="checkbox" id="csu1" onclick="checkingFunction(this)" name="napok[]"><label id="csu4" for="csu4">40</label></td>
              <td><input type="checkbox" id="csu1" onclick="checkingFunction(this)" name="napok[]"><label id="csu5" for="csu5">40</label></td>
              <td><input type="checkbox" id="csu1" onclick="checkingFunction(this)" name="napok[]"><label id="csu6" for="csu6">40</label></td>
            </tr>
        </tbody>
    </table>
    <input type="submit" class="button" value="Jelentkezés elküldése">
</form>

就像我说的,我想要一个数组。它的大小应该在24个元素(24个复选框)左右。稍后,我想检查每一个是否被检查,但我如何才能做所有这些事情?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-01-21 17:03:48

这方面的两种主要方法。一种方法是定义键:

代码语言:javascript
复制
name="napok[he1]"
name="napok[he2]"

然后检查一下它们:

代码语言:javascript
复制
if(isset($_POST['napok']['he1'])) { }

或设置值:

代码语言:javascript
复制
name="napok[]" value="he1"
name="napok[]" value="he2"

然后检查一下:

代码语言:javascript
复制
if(in_array('he1', $_POST['napok'])) { }

此外,这是不正确的(见上文):

代码语言:javascript
复制
$napok[] = $_POST['napok[]'];

isset($_POST['napok[0]'])
票数 0
EN

Stack Overflow用户

发布于 2019-01-21 17:04:01

删除额外的$_POST['napok[]'];括号,并尝试使用$_POST['napok']抓取所有复选框作为array。此外,您还可以传递napok数组的索引,如napok[he1]napok[he2]等,以便在检查检查哪个元素是否选中时更容易?

完整代码:

代码语言:javascript
复制
<form class="asd" id="checkbox" action="?"  method="post">
    <table>
      <thead>
          <tr>
            <th></th>
              <th>1.</th>
              <th>2.</th>
              <th>3.</th>
              <th>4.</th>
              <th>5.</th>
              <th>6.</th>
          </tr>
        </thead>

        <tbody>
            <tr>
              <th>Hétfő</th>
                <td><input type="checkbox" id="he1" onclick="checkingFunction(this)" name="napok[he1]"><label id="he1" for="he1">40</label></td>
                <td><input type="checkbox" id="he2" onclick="checkingFunction(this)" name="napok[he2]"><label id="he2" for="he2">40</label></td>
                <td><input type="checkbox" id="he3" onclick="checkingFunction(this)" name="napok[he3]"><label id="he3" for="he3">40</label></td>
                <td><input type="checkbox" id="he4" onclick="checkingFunction(this)" name="napok[he4]"><label id="he4" for="he4">40</label></td>
                <td><input type="checkbox" id="he5" onclick="checkingFunction(this)" name="napok[he5]"><label id="he5" for="he5">40</label></td>
                <td><input type="checkbox" id="he6" onclick="checkingFunction(this)" name="napok[he6]"><label id="he6" for="he6">40</label></td>
            </tr>
            <tr>
              <th>Kedd</th>
              <td><input type="checkbox" id="ke1" onclick="checkingFunction(this)" name="napok[ke1]"><label id="ke1" for="ke1">40</label></td>
              <td><input type="checkbox" id="ke2" onclick="checkingFunction(this)" name="napok[ke2]"><label id="ke2" for="ke2">40</label></td>
              <td><input type="checkbox" id="ke3" onclick="checkingFunction(this)" name="napok[ke3]"><label id="ke3" for="ke3">40</label></td>
              <td><input type="checkbox" id="ke4" onclick="checkingFunction(this)" name="napok[ke4]"><label id="ke4" for="ke4">40</label></td>
              <td><input type="checkbox" id="ke5" onclick="checkingFunction(this)" name="napok[ke5]"><label id="ke5" for="ke5">40</label></td>
              <td><input type="checkbox" id="ke6" onclick="checkingFunction(this)" name="napok[ke6]"><label id="ke6" for="ke6">40</label></td>
            </tr>
            <tr>
              <th>Szerda</th>
              <td><input type="checkbox" id="sze1" onclick="checkingFunction(this)" name="napok[sze1]"><label id="sze1" for="sze1">40</label></td>
              <td><input type="checkbox" id="sze2" onclick="checkingFunction(this)" name="napok[sze2]"><label id="sze2" for="sze2">40</label></td>
              <td><input type="checkbox" id="sze3" onclick="checkingFunction(this)" name="napok[sze3]"><label id="sze3" for="sze3">40</label></td>
              <td><input type="checkbox" id="sze4" onclick="checkingFunction(this)" name="napok[sze4]"><label id="sze4" for="sze4">40</label></td>
              <td><input type="checkbox" id="sze5" onclick="checkingFunction(this)" name="napok[sze5]"><label id="sze5" for="sze5">40</label></td>
              <td><input type="checkbox" id="sze6" onclick="checkingFunction(this)" name="napok[sze6]"><label id="sze6" for="sze6">40</label></td>
            <tr>
              <hr>
              <th>Csütörtök</th>
              <td><input type="checkbox" id="csu1" onclick="checkingFunction(this)" name="napok[csu1]"><label id="csu1" for="csu1">40</label></td>
              <td><input type="checkbox" id="csu1" onclick="checkingFunction(this)" name="napok[csu2]"><label id="csu2" for="csu2">40</label></td>
              <td><input type="checkbox" id="csu1" onclick="checkingFunction(this)" name="napok[csu3]"><label id="csu3" for="csu3">40</label></td>
              <td><input type="checkbox" id="csu1" onclick="checkingFunction(this)" name="napok[csu4]"><label id="csu4" for="csu4">40</label></td>
              <td><input type="checkbox" id="csu1" onclick="checkingFunction(this)" name="napok[csu5]"><label id="csu5" for="csu5">40</label></td>
              <td><input type="checkbox" id="csu1" onclick="checkingFunction(this)" name="napok[csu6]"><label id="csu6" for="csu6">40</label></td>
            </tr>
        </tbody>
    </table>
    <input type="submit" class="button" value="Jelentkezés elküldése">
</form>

<?php 
if (isset($_POST['napok'])) 
{
    print '<pre>';
    print_r($_POST['napok']); 
    print '</pre>';
}
?>

提交后的

代码语言:javascript
复制
Array
(
    [he4] => on
    [he6] => on
    [ke2] => on
    [ke5] => on
    [sze5] => on
    [csu3] => on
    [csu4] => on
)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54294547

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档