次のようなフォームがあるとします。
<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 のこの例のフォームには、名前がすべて項目である 4 つのチェック ボックスがあり、ユーザーが [送信] ボタンをクリックすると、Checker 関数が呼び出されます。 Checker が true を返した場合、フォームは送信されます。Checker が false を返した場合、フォームは送信されません。ここでの Checker 関数は、これから作成する前処理関数です。 HTML のヘッダー部分に次の 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 はチェック ボックスの情報を受け取り、選択されたすべてのチェック ボックスの値を文字列配列に結合できます。myphp でこのようなステートメントを使用します。 .php:
これらのオプションは配列に分割できます。オプションの値には区切り文字 (ここではスペース) を含めることはできないことに注意してください。