草庐IT

php - 循环一个复选框列表,其中一些可能未在 PHP 中选中

coder 2024-04-11 原文

我有一个这样的 HTML 架构:

链接:http://i.stack.imgur.com/9RRpx.jpg

复选框名称:Lunes 列 name = arrayLunes[] 然后是 Martes 列 name = arrayMartes[],等等...

首先,我想测试 Lunes 列表(星期一),如果我检查了 firstthird,数组将只有 [0]和 [1] 数组位置,但我想要例如:[0] = true, [1] = false, [2] = true, [3 .. x] = false

像这样的 PHP 代码,那显然是行不通的,因为如果一个复选框没有被选中,将不会通过 POST 发送,所以它会是 index offset error

for ($c = 0; $c < count($_POST['arrayLunes']); $c++)
    echo ($_POST['arrayLunes'][$c] == 'on' ? "YES" : "NO");

结论:所以,现在,$_POST['arrayLunes'] 变量只包含为了checkbox checked,我需要未检查的也在他各自的位置。

我该怎么做或如何模拟它?

编辑

我的 HTML 代码是这样的

<div style="margin-left: 5px; padding: 5px;">
    <input class="btnFranjas" type="button" value="- Quitar franja" onclick="removerFranjaCalendario();" />
    <input class="btnFranjas" type="button" value="+ Añadir franja" onclick="addFranjaCalendario();" />
    <input class="btnFranjas" type="button" value="Reestablecer" onclick="reestablecerFranja();" />
</div>
<form action="index.php?zona=plataforma&id=<?php echo $_GET['id']; ?>&acceso=<?php echo $_GET['acceso']; ?>" method="post">
    <table id="tablaCalendario">
        <thead>
            <th>Horario</th>
            <th>Lunes</th>
            <th>Martes</th>
            <th>Miércoles</th>
            <th>Jueves</th>
            <th>Viernes</th>
            <th>Sábado</th>
            <th>Domingo</th>
        </thead>
        <tbody>
            <tr style="background: #E0E6F8;">
                <td>
                    <table>
                        <tr>
                            <td><b>Inicio: </b></td>
                            <td>
                                Hora
                                <select id='arrayInicioHora[]' name='arrayInicioHora[]'>
<?php
                                    for ($c = 0; $c < 24; $c++)
                                        echo "    <option value='" . $c . "'>".($c > 9 ? $c : "0" . $c)."</option>";
?>
                                </select>
                            </td>
                            <td>
                                Minuto
                                <select id='arrayInicioMinuto[]' name='arrayInicioMinuto[]'>
<?php
                                    for ($c = 0; $c < 60; $c++)
                                        echo "<option value='" . $c . "'>".($c > 9 ? $c : "0" . $c)."</option>";
?>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td><b>Fin: </b></td>
                            <td>
                                Hora
                                <select id='arrayFinHora[]' name='arrayFinHora[]'>
<?php
                                    for ($c = 0; $c < 24; $c++)
                                        echo "<option value='" . $c . "'>".($c > 9 ? $c : "0" . $c)."</option>";
?>
                                </select>
                            </td>
                            <td>
                                Minuto
                                <select id='arrayFinMinuto[]' name='arrayFinMinuto[]'>
<?php
                                    for ($c = 0; $c < 60; $c++)
                                        echo "    <option value='" . $c . "'>".($c > 9 ? $c : "0" . $c)."</option>";
?>
                                </select>
                            </td>
                        </tr>
                    </table>
                </td>
                <td align="center"><input type="checkbox" name="arrayLunes[]" id="arrayLunes[]" value="0" /> <input type="text" placeholder="Valor" name="arrayValorLunes[]" id="arrayValorLunes[]" style="width: 60px;" /></td>
                <td align="center"><input type="checkbox" name="arrayMartes[]" id="arrayMartes[]" value="0" /> <input type="text" placeholder="Valor" name="arrayValorMartes[]" id="arrayValorMartes[]" style="width: 60px;" /></td>
                <td align="center"><input type="checkbox" name="arrayMiercoles[]" id="arrayMiercoles[]" value="0" /> <input type="text" placeholder="Valor" name="arrayValorMiercoles[]" id="arrayValorMiercoles[]" style="width: 60px;" /></td>
                <td align="center"><input type="checkbox" name="arrayJueves[]" id="arrayJueves[]" value="0" /> <input type="text" placeholder="Valor" name="arrayValorJueves[]" id="arrayValorJueves[]" style="width: 60px;" /></td>
                <td align="center"><input type="checkbox" name="arrayViernes[]" id="arrayViernes[]" value="0" /> <input type="text" placeholder="Valor" name="arrayValorViernes[]" id="arrayValorViernes[]" style="width: 60px;" /></td>
                <td align="center"><input type="checkbox" name="arraySabado[]" id="arraySabado[]" value="0" /> <input type="text" placeholder="Valor" name="arrayValorSabado[]" id="arrayValorSabado[]" style="width: 60px;" /></td>
                <td align="center"><input type="checkbox" name="arrayDomingo[]" id="arrayDomingo[]" value="0" /> <input type="text" placeholder="Valor" name="arrayValorDomingo[]" id="arrayValorDomingo[]" style="width: 60px;" /></td>
            </tr>
        </tbody>
    </table>
    <input class="orangebutton" type="submit" name="enviarCalendario" id="enviarCalendario" onclick="return confirmacionAccion();" value="Enviar calendario" />
</form>

还有我为这个样本编写的 javascript:http://pastebin.com/eKFwMFvD

最佳答案

有一个技巧可以让您始终收到复选框的值:

<input type="hidden" name="arrayLunes[1]" value="0">
<input type="checkbox" name="arrayLunes[1]" value="1">

<input type="hidden" name="arrayLunes[2]" value="0">
<input type="checkbox" name="arrayLunes[2]" value="1">

因此,如果复选框被选中,您将收到 1 作为值,否则您将收到 0

编辑:

就像丹尼尔说的,你有不止一个 arrayLunes。所以你必须手动添加一个索引到数组符号。见上文。

要遍历您的复选框,请执行以下操作:

foreach($_POST['arrayLunes'] as $val)
    echo $val ? "YES" : "NO";

关于php - 循环一个复选框列表,其中一些可能未在 PHP 中选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17574870/

有关php - 循环一个复选框列表,其中一些可能未在 PHP 中选中的更多相关文章

  1. ruby - 树顶语法无限循环 - 2

    我脑子里浮现出一些关于一种新编程语言的想法,所以我想我会尝试实现它。一位friend建议我尝试使用Treetop(Rubygem)来创建一个解析器。Treetop的文档很少,我以前从未做过这种事情。我的解析器表现得好像有一个无限循环,但没有堆栈跟踪;事实证明很难追踪到。有人可以指出入门级解析/AST指南的方向吗?我真的需要一些列出规则、常见用法等的东西来使用像Treetop这样的工具。我的语法分析器在GitHub上,以防有人希望帮助我改进它。class{initialize=lambda(name){receiver.name=name}greet=lambda{IO.puts("He

  2. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  3. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  4. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  5. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  6. ruby - RVM 使用列表[0] - 2

    是否有类似“RVMuse1”或“RVMuselist[0]”之类的内容而不是键入整个版本号。在任何时候,我们都会看到一个可能包含5个或更多ruby的列表,我们可以轻松地键入一个数字而不是X.X.X。这也有助于rvmgemset。 最佳答案 这在RVM2.0中是可能的=>https://docs.google.com/document/d/1xW9GeEpLOWPcddDg_hOPvK4oeLxJmU3Q5FiCNT7nTAc/edit?usp=sharing-知道链接的任何人都可以发表评论

  7. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  8. ruby - 为什么 SecureRandom.uuid 创建一个唯一的字符串? - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?

  9. ruby - RuntimeError(自动加载常量 Apps 多线程时检测到循环依赖 - 2

    我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("

  10. ruby-on-rails - Rails - 从另一个模型中创建一个模型的实例 - 2

    我有一个正在构建的应用程序,我需要一个模型来创建另一个模型的实例。我希望每辆车都有4个轮胎。汽车模型classCar轮胎模型classTire但是,在make_tires内部有一个错误,如果我为Tire尝试它,则没有用于创建或新建的activerecord方法。当我检查轮胎时,它没有这些方法。我该如何补救?错误是这样的:未定义的方法'create'forActiveRecord::AttributeMethods::Serialization::Tire::Module我测试了两个环境:测试和开发,它们都因相同的错误而失败。 最佳答案

随机推荐