设置未选中复选框php的值

Setting value of unchecked checkbox php(设置未选中复选框php的值)
本文介绍了设置未选中复选框php的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有这个表格

<tr>
    <td>
        <input type="hidden" name="po[]" value="<?php echo $ord['id'] ?>" />
        <input type="hidden" name="acts[]" value="1" />
        <input id="acts" value='2' type="checkbox" <?php echo $check ?> name="acts[]" />
    </td>
    <td><img src="<?php echo $pnme['icon'] ?>" style="height: 25px; width: 25px; vertical-align: middle;" /><?php echo $pnme['name'] ?></td>
    <td><input id="per" style="background: #BDBDBD; width: 100px;" name="per[]" size="5" type="text" class="small" value="<?php echo $ord['fee'] ?>" /></td>
    <td><input id="dol" style="background: #BDBDBD; width: 100px;" name="dol[]" size="5" type="text" class="small" value="<?php echo $ord['cost'] ?>" /></td>
</tr>

如果未选中复选框,我试图让它传递不同的值,我有一个 hidden 输入,但它没有在我需要的地方正确传递值,当表单帖子这就是我处理信息的方式

I am trying to get it to pass a different value if the checkbox is not checked, I have a hidden input but it does not pass the values correctly where I need it to, when the form posts this is how I handle the information

    $po = implode(",", $_POST['po']);
    $po = explode(",", $po);

    $fee = implode(",", $_POST['per']);
    $fee = explode(",", $fee);

    $co = implode(",", $_POST['dol']);
    $co = explode(",", $co);

    $act = implode(",", $_POST['acts']);
    $act = explode(",", $act);
    print_r($act);
    for ($i = 0; ; $i++) {
    if ($i > count($po) - 1) {
        break;
    }

例如,如果我检查每个框,它会像这样设置数组

for example if I check every box it sets the array like this

Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 2 [4] => 1 [5] => 2 [6] => 1 [7] => 2 [8] => 1 [9] => 2 [10] => 1 [11] => 2 [12] => 1 [13] => 2 [14] => 1 [15] => 2 [16] => 1 [17] => 2 [18] => 1 [19] => 2 [20] => 1 [21] => 2 ) 

即使任何一个都应该有 2 的值

even though ever single one should have the value of 2

我添加了这个试图将初始值设置为 1 认为如果选中该框它将覆盖该值,但情况似乎并非如此

I added this trying to set the initial value to 1 thinking that if the box is checked it would overwrite the value, but this doesn't seem to be the case

我不确定如何处理这种情况并使其与我的处理代码一起工作.

I am not sure how to handle this situation and make it work with my processing code.

推荐答案

[] 数组约定动态创建下一个索引,所以如果你有:

The [] array convention dynamically creates the next index, so if you have:

name="acts[]"
name="acts[]"

然后你得到:

acts[0]
acts[1]

需要保存checked和unchecked值的同名隐藏和checkbox输入需要有相同的索引,所以指定如下:

The hidden and checkbox inputs with the same name that need to hold the checked and unchecked value need to have the same index, so specify like:

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

<!-- more checkboxes -->

<input type="hidden" name="acts[10]" value="1" />
<input type="checkbox" name="acts[10]" value="2" />

这将为您提供一个 $_POST['acts'] 数组,其中包含 11 个项目,索引 0-10,值为 12取决于它是否被检查.

This will give you a $_POST['acts'] array with 11 items, index 0-10 with the value either 1 or 2 depending on whether it was checked or not.

另外,我不知道你在用 implode()explode() 做什么.如果您只是想重新索引数字键,请使用 array_values($_POST['acts']).

Also, I have no idea what you are doing with the implode() and explode(). If you are just trying to reindex the numeric keys then use array_values($_POST['acts']).

这篇关于设置未选中复选框php的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)