PHP を正しく実装してチェックボックスの値を取得する方法は、初心者にとってはまだ比較的馴染みがありません。ここでは、関連する実装方法を詳しく紹介し、皆様のお役に立てれば幸いです。
weeks の後の括弧を省略しないでください。省略すると、PHP を使用するときに最後の値しか取得できなくなります。その後、PHP は次のように非常に簡単に処理できます:
チェックボックスの値を取得する PHP メソッド 1:
<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><span class="tag-name">count<br /></span><span>($weeks);$i++) </span></li><li class="alt"><span>echo $weeks[1]."</span><span class="tag"><</span><span> </span><span class="tag-name">br</span><span class="tag">></font></strong></span><span>"; </span></span></li> </ol> <p> チェックボックスの値を取得する PHP メソッド 2: <strong></strong></p> <pre class="brush:php;toolbar:false"><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>
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span> ?PHP </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><span> $size; $i++){ </span></li><li><span>echo $array[$i]."</span><span class="tag"><</span><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><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> </li> <li class="alt"> <span class="tag"><</span><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><span class="tag-name">br</span><span class="tag">></span><span> </span> </li> <li> <span class="tag"><</span><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><span class="tag-name">br</span><span class="tag">></span><span> </span> </li> <li class="alt"> <span class="tag"><</span><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><span class="tag-name">br</span><span class="tag">></span><span> </span> </li> <li> <span class="tag"><</span><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><span class="tag-name">br</span><span class="tag">></span><span> </span> </li> <li class="alt"> <span class="tag"><</span><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> </li> <li> <span class="tag"><</span><span> /form</span><span class="tag">></span><span> </span> </li> </ol>