php 表单发布时如何获取复选框元素中未选中复选框的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19239536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:05:17  来源:igfitidea点击:

How get value for unchecked checkbox in checkbox elements when form posted?

phparraysforms

提问by Fredy

I have a form like below :

我有一个像下面这样的表格:

<form action="" method="post">
    <input type="checkbox" id="status_1" name="status_1" value="1" />
    <input type="checkbox" id="status_2" name="status_2" value="1" />
    <input type="checkbox" id="status_3" name="status_3" value="1" />
</form>

When i check all checkbox and post the form, the result is like this:

当我选中所有复选框并发布表单时,结果是这样的:

Array ([status_3] => 1 [status_2] => 1 [status_1] => 1 ) 

Then i uncheck second checkbox and post the form, the result is like this:

然后我取消选中第二个复选框并发布表单,结果如下:

Array ( [status_3] => 1 [status_1] => 1 ) 

Is it possible to make result like this below when i uncheck second checkbox :

当我取消选中第二个复选框时,是否有可能产生如下结果:

Array ( [status_3] => 1 [status_2] => 0 [status_1] => 1 ) 

There are ideas to do it?

有没有做的想法?

回答by Royal Bg

First way - hidden fields (disadvantage: the user can manipulate the value of the field (but one can manipulate the value of the checkbox too, so it's not really a problem, if you only expect 1 or 0))

第一种方式 - 隐藏字段(缺点:用户可以操纵字段的值(但也可以操纵复选框的值,所以这不是真正的问题,如果你只期望 1 或 0))

<form action="" method="post">
<input type="hidden" name="status_1" value="0" />
<input type="checkbox" id="status_1" name="status_1" value="1" />
<input type="hidden" name="status_2" value="0" />
<input type="checkbox" id="status_2" name="status_2" value="1" />
<input type="hidden" name="status_3" value="0" />
<input type="checkbox" id="status_3" name="status_3" value="1" />
<input type="submit" />
</form>
<?php
var_dump($_POST);
/*
 * checking only the second box outputs:
 * 
 * array (size=3)
  'status_1' => string '0' (length=1)
  'status_2' => string '1' (length=1)
  'status_3' => string '0' (length=1)
 */

Second way - to assign default value for non-set indexes:

第二种方法 - 为非设置索引分配默认值:

<form action="" method="post">
<input type="checkbox" id="status_1" name="status_1" value="1" />
<input type="checkbox" id="status_2" name="status_2" value="1" />
<input type="checkbox" id="status_3" name="status_3" value="1" />
<input type="submit" />
</form>
<?php
for($i = 1; $i<=count($_POST); $i++) {
    $_POST["status_$i"] = isset($_POST["status_$i"]) ? $_POST["status_$i"] : 0;
}
var_dump($_POST);

/**
 * Here we will be checking only the third checkbox:
 * 
 * array (size=3)
  'status_3' => string '1' (length=1)
  'status_1' => int 0
  'status_2' => int 0
 */

回答by nithin

I think adding hidden fields like this will work

我认为添加这样的隐藏字段会起作用

<input type="hidden" id="status_1_" name="status_1"  value="0">
<input type="checkbox" id="status_1" name="status_1" value="1" />

<input type="hidden" id="status_2_" name="status_2" value="0">
<input type="checkbox" id="status_2" name="status_2" value="1" />

<input type="hidden" id="status_3_" name="status_3" value="0">
<input type="checkbox" id="status_3" name="status_3" value="1" />

回答by Somy A

I thinks it impossible to get array like what you want from html forms. But this some tricks can be used:

我认为不可能从 html 表单中获得你想要的数组。但是可以使用一些技巧:

$defaultForm = array(
'status_1' => 0,
'status_2' => 0,
'status_3' => 0, 
);

// example array from $_POST
$form = array(
'status_1' => 1,
'status_3' => 1, 
);

$form = array_merge($defaultForm, $form);

Result:

结果:

array(3) {

'status_1' => int(1)
'status_2' => int(0)
'status_3' => int(1)

}

数组(3){

'status_1' => int(1)
'status_2' => int(0)
'status_3' => int(1)

}

回答by luttkens

Try this. If the checkbox is not checked, then the hidden field with the same name will be passed instead.

尝试这个。如果未选中该复选框,则将传递具有相同名称的隐藏字段。

<form action="" method="post">
  <input type="hidden" id="hidden_status_1" name="status_1" value="0" />
  <input type="checkbox" id="status_1" name="status_1" value="1" />
  <input type="hidden" id="hidden_status_2" name="status_2" value="0" />
  <input type="checkbox" id="status_2" name="status_2" value="1" />
  <input type="hidden" id="hidden_status_3" name="status_3" value="0" />
  <input type="checkbox" id="status_3" name="status_3" value="1" />
</form>

回答by Jay Harris

the question may already be answered but i just wanted to take a stab at it...server side only solution:

问题可能已经得到解答,但我只是想尝试一下……服务器端唯一的解决方案:

$p = $_POST;
$a = array();
$a['status_3'] = (int) ($p['status_3'] === 1);
$a['status_2'] = (int) ($p['status_2'] === 1);
$a['status_1'] = (int) ($p['status_1'] === 1);

Testing

测试

 // if input is Array("status_1"=>1) output will be
 Array ( [status_1] => 1 [status_3] => 0 [status_2] => 0 )

 // if input is Array("status_1"=>1, "status_2"=>1) output will be
 Array ( [status_1] => 1 [status_3] => 0 [status_2] => 1)

回答by Fredy

Thanks all. Thank to @RoyalBg give me solution. Like this :

谢谢大家。感谢@RoyalBg 给我解决方案。像这样 :

<input type="hidden" name="status_1" value="0" />
<input type="checkbox" id="status_1" name="status_1" value="1" /> Check 1 <br />
<input type="hidden" name="status_2" value="0" /> 
<input type="checkbox" id="status_2" name="status_2" value="1" /> Check 2 <br />
<input type="hidden" name="status_3" value="0" />
<input type="checkbox" id="status_3" name="status_3" value="1" /> Check 3 <br />

It's work perfectly.. :)

它完美地工作.. :)

回答by Inderjeet Singh

<!--html code-->                                                                  
<input type="checkbox" name="correct" value="1">Option 1
<input type="checkbox" name="correct" value="2">Option 2
<input type="checkbox" name="correct" value="3">Option 3
<input type="checkbox" name="correct" value="4">Option 4                                   

//php code in function called on form submit       
  public function addOptions(Request $request)
   {
    $option = array('1' => 0,'2'=>0,'3'=>0,'4'=>0 );
    $option[$request->correct] = 1;
    return $option;
   }

回答by Mandip Darji

try below code

试试下面的代码

    $myresult = array();

    if(!isset($_POST['status_1'])){ 
        $myresult['status_1'] = 0;
    }
    if(!isset($_POST['status_2'])){ 
        $myresult['status_2'] = 0;
    }
    if(!isset($_POST['status_3'])){ 
        $myresult['status_3'] = 0;
    }

    echo "<pre>";
    print_r($myresult);
    echo "</pre>";
    exit;

回答by M.Athish krishna

Try this one:

试试这个:

for ($i = 1; $i<=3; $i++) {
    $_POST["status_$i"] = isset($_POST["status_$i"]) ? $_POST["status_$i"] : 0; // 0 if not set
}

var_dump($_POST);

回答by tom

Assuming we are using checkboxes with zeros or ones...

假设我们使用带有零或一的复选框......

Using a hidden checkbox with a zero value is just a work-around. Another work around would be to add 0 to the value when receiving the post or get.

使用具有零值的隐藏复选框只是一种解决方法。另一种解决方法是在接收帖子或获取时将 0 添加到值中。

Example:

例子:

$chkbx1 = $_POST['chckbx1']; $chkbx1 += 0;

$chkbx1 = $_POST['chckbx1']; $chkbx1 += 0;

This takes a NULL value an turns it into a zero, but if the value is one, as in its checked, then the value stays the same.

这需要一个 NULL 值并将其变成零,但如果该值为 1,如在其检查中,则该值保持不变。

The real issue here isn't inventing a work-around. Its understanding why this is happening. Older versions of mySQL takes null values and converts them into a zero. In newer versions, you must disable strict mode and then a work-around is not needed.

这里真正的问题不是发明一种变通方法。它理解为什么会发生这种情况。旧版本的 mySQL 接受空值并将它们转换为零。在较新的版本中,您必须禁用严格模式,然后就不需要解决方法了。