An easy way to receive check box information in PHP_PHP Tutorial

WBOY
Release: 2016-07-15 13:29:21
Original
870 people have browsed it

Suppose there is a form like this:

<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>
Copy after login

The form in this example of PHP receiving checkbox information has four checkboxes whose names are all items. When the user When the Submit button is clicked, the Checker function will be called, and if the Checker returns true, the form will be submitted, if it returns false, the form will not be submitted. The Checker function here is the preprocessing function we want to write. Add the following javascript in the header part of the HTML:

<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>
Copy after login

In this way, PHP can receive the checkbox information and combine the values ​​of all selected checkboxes into a string array, in myphp. PHP uses such a statement:

to separate these options into an array. It should be noted that the value in the option cannot contain delimiters (here, spaces).


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446372.htmlTechArticleSuppose there is such a form: <form name = "form1" id = "form1" method = "post" action = "myphp.php" onSubmit = "returnChecker()" > <input type = "checkbox" name = "item" va...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template