Home >Backend Development >PHP Tutorial >Explore PHP to get checkbox value_PHP tutorial

Explore PHP to get checkbox value_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:34:51860browse

How to correctly implement PHP to obtain the checkbox value is still relatively unfamiliar to beginners. Here we will introduce the relevant implementation methods in detail, hoping to be helpful to everyone.

  1. < input type="checkbox" name="weeks[]"
    id="weeks" value=1>
  2. < input type="checkbox " name="weeks[]"
    id="weeks" value=2>
  3. < input type="checkbox" name="weeks[]"
    id="weeks" value=3>


The square brackets after weeks must not be missed, otherwise only the last value can be obtained when using PHP. After that, PHP can handle it very easily, as follows:

PHP method one to get the checkbox value:

<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">></span><span>";  </span>
</li>
</ol>

PHP method two to get the checkbox value:

<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>

Forms are often used, and check boxes should be used frequently. But in PHP, it is different from other scripting languages. The name of the check box must be appended with [], and then used to obtain it through an array loop.

<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>

The above is an introduction to the two methods of obtaining the checkbox value in PHP.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445960.htmlTechArticleHow to correctly implement PHP to obtain the checkbox value is still relatively unfamiliar to beginners. Here we will introduce the relevant implementation methods in detail, hoping to be helpful to everyone...
Statement:
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