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

PHP获取checkbox值

WBOY
WBOYOriginal
2016-06-23 14:30:29821browse

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

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

方法一:

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

 

方法二:

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

 

经常用到表单,其中复选框要经常用。但在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> 

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
Previous article:php知识必备Next article:PHP 按位与或 (^ 、&)