Heim >Backend-Entwicklung >PHP-Tutorial >PHP接收复选框信息的简便方法讲解_PHP教程

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

WBOY
WBOYOriginal
2016-07-15 13:29:21899Durchsuche

假设有这样一个表单:

<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...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn