Home  >  Article  >  Backend Development  >  PHP method example code to get the values ​​​​of all check boxes in the form

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

怪我咯
怪我咯Original
2017-07-11 14:17:041594browse

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

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;
}

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!

Statement:
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