探讨PHP获取checkbox值_PHP教程

WBOY
發布: 2016-07-15 13:34:51
原創
836 人瀏覽過

如何才能正确的实现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>
登入後複製

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>
登入後複製

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

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


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445960.htmlTechArticle如何才能正确的实现PHP获取checkbox值对于初学者来说还是比较陌生的。在这里我们将为大家详细介绍相关的实现方法,希望对大家有所帮助...
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板