Php 如何同时获取多个复选框ID和值

Php 如何同时获取多个复选框ID和值,php,html,post,checkbox,Php,Html,Post,Checkbox,如何通过同时发布多个复选框ID和值来获取?下面的代码显示发送数据的html <form action="" method="post"> first<input type="checkbox" name="first[]" id="first'<?php echo $data; ?>'" value="first" /> second<input type="checkbox" name="first[]" id="second'<?php

如何通过同时发布多个复选框ID和值来获取?下面的代码显示发送数据的html

<form action="" method="post">
  first<input type="checkbox" name="first[]" id="first'<?php echo $data; ?>'" value="first" />
  second<input type="checkbox" name="first[]" id="second'<?php echo $data2; ?>'" value="second" />
  third<input type="checkbox" name="first[]" id="third'<?php echo $data3; ?>'" value="third" />
  <input type="submit" value="submit">
</form>


首先您可以执行以下操作:

<form action="" method="post">
  first<input type="checkbox" name="first[0][value]" id="first[]" value="first" />
  <input type="hidden" name="first[0][id]" value="first[]">
  second<input type="checkbox" name="first[1][value]" id="second[]" value="second" />
  <input type="hidden" name="first[1][id]" value="second[]">
  third<input type="checkbox" name="first[2][value]" id="third[]" value="third" />
  <input type="hidden" name="first[2][id]" value="third[]">
  <input type="submit" value="submit">
</form>

第一
第二
第三
在后端:

foreach($_POST['first'] as $value){
  echo 'VALUE: '.$value['value'].'<br/>';
  echo 'ID: '.$value['id'].'<br/>';
}
foreach($\u POST['first']作为$value){
回显'VALUE:'.$VALUE['VALUE'].
; 回显'ID:'.$value['ID'].
; }
您可以执行以下操作:

<form action="" method="post">
  first<input type="checkbox" name="first[0][value]" id="first[]" value="first" />
  <input type="hidden" name="first[0][id]" value="first[]">
  second<input type="checkbox" name="first[1][value]" id="second[]" value="second" />
  <input type="hidden" name="first[1][id]" value="second[]">
  third<input type="checkbox" name="first[2][value]" id="third[]" value="third" />
  <input type="hidden" name="first[2][id]" value="third[]">
  <input type="submit" value="submit">
</form>

第一
第二
第三
在后端:

foreach($_POST['first'] as $value){
  echo 'VALUE: '.$value['value'].'<br/>';
  echo 'ID: '.$value['id'].'<br/>';
}
foreach($\u POST['first']作为$value){
回显'VALUE:'.$VALUE['VALUE'].
; 回显'ID:'.$value['ID'].
; }
如果要从输入中获取
id
值,请使用
id
作为名称数组中的键

<input type="checkbox" name="first[first]" .../>
<input type="checkbox" name="first[second]" .../>
<input type="checkbox" name="first[third]" .../>

如果要从输入中获取
id
值,请在名称数组中使用
id
作为键

<input type="checkbox" name="first[first]" .../>
<input type="checkbox" name="first[second]" .../>
<input type="checkbox" name="first[third]" .../>

id
不会发送到服务器。您在这里实际想要实现什么以及为什么?元素的ID不会传输到后端。您必须添加一个专用的
,并将其值设置为ID的值。如果ID与值相同-有什么意义?此外,我不认为
first[]
在HTML中是有效的ID。如果您想要
ID
,则将其添加到名称数组->
name=“first[first]”
/
name=“first[second]”
/
name=“first[third]”
name=“first[1]”
/
name=“first[2]”
/
name=“first[3]”
。然后您可以在循环中获取它->
foreach($\u POST['first']as$id=>$value){echo'id:'.$id.'value:'.$value.
;}
不会将
id
发送到服务器。您在这里实际想要实现什么以及为什么?元素的ID不会传输到后端。您必须添加一个专用的
,并将其值设置为ID的值。如果ID与值相同-有什么意义?此外,我不认为
first[]
在HTML中是有效的ID。如果您想要
ID
,则将其添加到名称数组->
name=“first[first]”
/
name=“first[second]”
/
name=“first[third]”
name=“first[1]”
/
name=“first[2]”
/
name=“first[3]”
。然后你可以在你的循环中得到它->
foreach($_POST['first']as$id=>$value){echo'id:'.$id.'value:'.$value.
;}
现在我明白你的意思了)这个也对我有用谢谢@不客气。我真诚地希望您不要在项目中使用我的示例,因为这只能作为最后的手段使用,但我确实希望我能够向您展示一些关于HTML/PHP中数组的新内容。祝你好运我用了不同的方式,但你的方式对我来说是新的,谢谢你展示了新的方式!)现在我明白你的意思了)这个对我也有用!)谢谢@不客气。我真诚地希望您不要在项目中使用我的示例,因为这只能作为最后的手段使用,但我确实希望我能够向您展示一些关于HTML/PHP中数组的新内容。祝你好运我用了不同的方式,但你的方式对我来说是新的,谢谢你展示了新的方式!)