从bootstrap表单生成器创建的复选框中获取数据
问题描述:
I have a set up checkboxes that look like the below
<div class="control-group">
<label class="control-label" for="environment">Environment</label>
<div class="controls">
<label class="checkbox inline" for="environment-0">
<input type="checkbox" name="environment['com1']" id="environment-0" value="1">
Com1
</label>
<label class="checkbox inline" for="environment-1">
<input type="checkbox" name="environment['com2']" id="environment-1" value="1">
Com2
</label>
<label class="checkbox inline" for="environment-2">
<input type="checkbox" name="environment['com3']" id="environment-2" value="1">
Com3
</label>
</div>
</div>
When I return the $_POST
data I get the following
array(7) {
["environment"]=>
array(2) {
["'com1'"]=>
string(1) "1"
["'com2'"]=>
string(1) "1"
}
I'm somewhat new to dealing with arrays and am confused how to reference the string found in com1
. I'm trying $envCom1 = $_POST["environment['com1']"];
but getting the error Undefined index: environment['com1']
答
$var = $_POST["environment"]
is an array. So you should access value with 'com1' index of this array like,
$var = $_POST["environment"];
echo $var["'com1'"];
OR
echo $_POST["environment"]["'com1'"];