Home > php教程 > php手册 > 探讨PHP获取checkbox值

探讨PHP获取checkbox值

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 11:11:43
Original
1060 people have browsed it

如何才能正确的实现PHP获取checkbox值对于初学者来说还是比较陌生的。在这里我们将为大家详细介绍相关的实现方法,希望对大家有所帮助。

  1.  input type="checkbox" name="weeks[]" 
    id="weeks" value=1>   
  2.  input type="checkbox" name="weeks[]" 
    id="weeks" value=2>   
  3.  input type="checkbox" name="weeks[]" 
    id="weeks" value=3>  


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

PHP获取checkbox值方法一:

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute">weeks</span><span> = $_POST['weeks'];   </span></span></li>
<li>
<span>for($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>;$i</span><span class="tag"><span> </span><span class="tag-name">count<br></span><span>($weeks);$i++)   </span></span>
</li>
<li class="alt">
<span>echo $weeks[1]."</span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span>";  </span></span>
</li>
</ol>
Copy after login

PHP获取checkbox值方法二:

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute">array</span><span> = $this-</span><span class="tag">></span><span>request-</span><span class="tag">><br></span><span>getParameter("weeks[]");   </span></span></li>
<li>
<span>$</span><span class="attribute">str</span><span> =</span><span class="attribute-value">implode</span><span>(',',$array);   </span>
</li>
<li class="alt"><span>echo $str; </span></li>
</ol>
Copy after login

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

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

以上就是PHP获取checkbox值的两种方法介绍。


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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template