> 백엔드 개발 > PHP 튜토리얼 > PHP接收复选框信息的简便方法讲解_PHP教程

PHP接收复选框信息的简便方法讲解_PHP教程

WBOY
풀어 주다: 2016-07-15 13:29:21
원래의
886명이 탐색했습니다.

假设有这样一个表单:

<ol class="dp-xml">
<li class="alt"><span><span><form </span><span class="attribute"><font color="#ff0000">name</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"form1"</font></span><span> </span><span class="attribute"><font color="#ff0000">id</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"form1"</font></span><span> </span><span class="attribute"><font color="#ff0000">method</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"post"</font></span><span> </span><span class="attribute"><font color="#ff0000">action</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"myphp.php"</font></span><span> </span><span class="attribute"><font color="#ff0000">onSubmit</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"return Checker()"</font></span><span>>   </span></span></li>
<li class="">
<span><input </span><span class="attribute"><font color="#ff0000">type</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"checkbox"</font></span><span> </span><span class="attribute"><font color="#ff0000">name</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"item"</font></span><span> </span><span class="attribute"><font color="#ff0000">value</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"1"</font></span><span>>1<br>   </span>
</li>
<li class="alt">
<span><input </span><span class="attribute"><font color="#ff0000">type</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"checkbox"</font></span><span> </span><span class="attribute"><font color="#ff0000">name</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"item"</font></span><span> </span><span class="attribute"><font color="#ff0000">value</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"2"</font></span><span>>2<br>   </span>
</li>
<li class="">
<span><input </span><span class="attribute"><font color="#ff0000">type</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"checkbox"</font></span><span> </span><span class="attribute"><font color="#ff0000">name</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"item"</font></span><span> </span><span class="attribute"><font color="#ff0000">value</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"3"</font></span><span>>3<br>   </span>
</li>
<li class="alt">
<span><input </span><span class="attribute"><font color="#ff0000">type</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"checkbox"</font></span><span> </span><span class="attribute"><font color="#ff0000">name</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"item"</font></span><span> </span><span class="attribute"><font color="#ff0000">value</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"4"</font></span><span>>4<br>   </span>
</li>
<li class="">
<span><input </span><span class="attribute"><font color="#ff0000">type</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"hidden"</font></span><span> </span><span class="attribute"><font color="#ff0000">name</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"items"</font></span><span> </span><span class="attribute"><font color="#ff0000">value</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">""</font></span><span>>   </span>
</li>
<li class="alt">
<span><input </span><span class="attribute"><font color="#ff0000">type</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"submit"</font></span><span> </span><span class="attribute"><font color="#ff0000">value</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"Submit"</font></span><span>>   </span>
</li>
<li class=""><span></form>  </span></li>
</ol>
로그인 후 복사

这个PHP接收复选框信息的例子中的表单有四个名字都是item的复选框,当用户单击Submit按钮的时候,Checker函数会被调用,并且如果Checker返回true表单就被提交,返回false表单就不会被提交。这里Checker函数就是我们要编写的预处理函数。在HTML的header部分添加下面的javascript:

<ol class="dp-xml">
<li class="alt"><span><span><script </span><span class="attribute"><font color="#ff0000">language</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"javascript"</font></span><span>>   </span></span></li>
<li class=""><span><!--   </span></li>
<li class="alt"><span>function Checker()   </span></li>
<li class=""><span>{   </span></li>
<li class="alt">
<span> </span><span class="attribute"><font color="#ff0000">form1.items.value</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">""</font></span><span>;   </span>
</li>
<li class="">
<span> if ( !form1.item.length ) // 只有一个复选框,</span><span class="attribute"><font color="#ff0000">form1.item.length</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">undefined</font></span><span>   </span>
</li>
<li class="alt"><span> {   </span></li>
<li class=""><span>if ( form1.items.checked )   </span></li>
<li class="alt">
<span> </span><span class="attribute-value"><font color="#0000ff">form1</font></span><span class="attribute"><font color="#ff0000">form1.items.value</font></span><span> = form1.item.value;   </span>
</li>
<li class=""><span> }   </span></li>
<li class="alt"><span> else   </span></li>
<li class=""><span> {   </span></li>
<li class="alt">
<span>for ( </span><span class="attribute"><font color="#ff0000">i</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">0</font></span><span> ; i < form1.item.length ; i++ )   </span>
</li>
<li class=""><span>{   </span></li>
<li class="alt"><span> if ( form1.item(i).checked ) // 复选框中有选中的框   </span></li>
<li class=""><span> {   </span></li>
<li class="alt">
<span></span><span class="attribute-value"><font color="#0000ff">form1</font></span><span class="attribute"><font color="#ff0000">form1.items.value</font></span><span> = form1.item(i).value;   </span>
</li>
<li class="">
<span>for ( </span><span class="attribute"><font color="#ff0000">j</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">i</font></span><span> + 1 ; j < form1.item.length ; j++ )   </span>
</li>
<li class="alt"><span>{   </span></li>
<li class=""><span> if ( form1.item(j).checked )   </span></li>
<li class="alt"><span> {   </span></li>
<li class=""><span>form1.items.value += " "; //用空格做分割符   </span></li>
<li class="alt"><span>form1.items.value += form1.item(j).value;   </span></li>
<li class=""><span> }   </span></li>
<li class="alt"><span>}   </span></li>
<li class=""><span>break;   </span></li>
<li class="alt"><span> }   </span></li>
<li class=""><span>}   </span></li>
<li class="alt"><span> }   </span></li>
<li class=""><span> return true;   </span></li>
<li class="alt"><span>}   </span></li>
<li class=""><span>-->   </span></li>
<li class="alt"><span></script>   </span></li>
</ol>
로그인 후 복사

这样就PHP接收复选框信息可以把所有选中的复选框的value组合成为一个字符串数组,在myphp.php使用这样的语句:

就可以把这些选项分离出来成为数组。需要注意的是选项中的value不能包含分割符(这里是空格)。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446372.htmlTechArticle假设有这样一个表单: <form name = "form1" id = "form1" method = "post" action = "myphp.php" onSubmit = "returnChecker()" > <input type = "checkbox" name = "item" va...
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿