PHP method example code to get the values ​​​​of all check boxes in the form

怪我咯
Release: 2023-03-13 14:54:02
Original
1583 people have browsed it

php Get the formCheckboxThe value of checkbox is different from other development languages. This article introduces how to get the value of all checkboxes in the form in php. Friends who need it can refer to this article. An introduction to php checkbox values.

Generally speaking, only the value of the last check box is always obtained in PHP, so how can we obtain the values ​​of all check boxes?

The solution is as follows:

Part of the code of the form is as follows:

<input type="checkbox" name="frm_tag[]" value="1">1
<input type="checkbox" name="frm_tag[]" value="2">2
<input type="checkbox" name="frm_tag[]" value="3">3
<input type="checkbox" name="frm_tag[]" value="4">4
<input type="checkbox" name="frm_tag[]" value="5">5
Copy after login

Pay attention to the name attribute of the input above. The content of each attribute is the same, and They are all frm_tag[], and the reason for adding [] is to make the content of frm_tag become an array and be passed in the form.

Note: The square brackets after frm_tag must not be missed, otherwise only the last value can be obtained when using PHP.

The code for php to process the data passed by POST is as follows:

$str_tag = "";
$frm_tag = $_POST[&#39;frm_tag&#39;];
for($i=0;$i<count($frm_tag);$i++){
  echo $frm_tag[$i]."<br>";
  if($i==0)
   $str_tag = $frm_tag[$i];
  else
   $str_tag = $str_tag.",".$frm_tag[$i];
}
echo $str_tag;
}
Copy after login

The above is the detailed content of PHP method example code to get the values ​​​​of all check boxes in the form. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!