Home > Backend Development > PHP Tutorial > PHP获取checkbox值

PHP获取checkbox值

WBOY
Release: 2016-06-23 14:30:29
Original
860 people have browsed it

< input type="checkbox" name="weeks[]"  id="weeks" value=1>   < input type="checkbox" name="weeks[]"  id="weeks" value=2>   < input type="checkbox" name="weeks[]"  id="weeks" value=3> 
Copy after login

weeks后的中括号不可漏,否则用PHP获取的时候只能取到最后一个值。之后PHP就很好处理了,如下:

方法一:

$weeks = $_POST['weeks'];   for($i=0;$i< count($weeks);$i++)       echo $weeks[$i]."< br>"; 
Copy after login

方法二:

$array = $this->request->getParameter("weeks[]");   $str =implode(',',$array);   echo $str; 
Copy after login

经常用到表单,其中复选框要经常用。但在PHP中与其他的脚本语言不太一样,复选框的名称后面必须加上[],然后用数组循环取得。

< ?PHP      if(!empty($_POST["t1"])){              $array = $_POST["t1"];              $size = count($array);              for($i=0; $i< $size; $i++){                    echo $array[$i]."< br>";              }      }  ?>     < form method=post action="" name="form1">     < input type="checkbox"  name="t1[]" value="篮球">篮球< br>     < input type="checkbox"  name="t1[]" value="足球">足球< br>     < input type="checkbox"  name="t1[]" value="乒乓球">乒乓球< br>     < input type="checkbox"  name="t1[]" value="排球">排球< br>     < input type="submit">     < /form> 
Copy after login

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