How to get the values of multiple checkboxes in PHP

Source: Internet
Author: User

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


Weeks after the middle bracket is not leaky, otherwise you can only fetch the last value when you get it in PHP. After that, PHP is very well handled, as follows:

PHP Gets the checkbox value method one:

    1. $weeks = $_post[' weeks '];
    2. For ($i=0; $i< count
      ($weeks); $i + +)
    3. echo $weeks [1]. "  < br> ";

PHP Gets the checkbox value method two:

    1. $Array = $this->request->
      GetParameter ("weeks[]");
    2. $str =implode (', ', $array);
    3. Echo $str;

Use the form frequently, where the check box is used frequently. However, unlike other scripting languages in PHP, the name of the check box must be appended with [] and then used as an array loop.

    1. <? Php
    2. if (!empty ($_post["T1")) {
    3. $Array = $_post["T1"];
    4. $size = count ($array);
    5. For ($i=0; $i< $size; $i + +) {
    6. echo $array [$i]. "  < br> ";
    7. }
    8. }
    9. ?>
    10. < form method=post action= "" name= "Form1">
    11. < input type="checkbox"
      Name="t1[" value="basketball"> Basketball < br>
    12. < input type="checkbox"
      Name="t1[" value="soccer"> Soccer < br>
    13. < input type="checkbox"
      Name="t1[]" value="ping-pong"> Table Tennis < br>
    14. < input type="checkbox"
      Name="t1[" " value=" Volleyball> Volleyball < br>
    15. < input type="Submit">
    16. </form>

How to get the values of multiple checkboxes in PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.