PHP getting checkbox value _ PHP Tutorial

Source: Internet
Author: User
Explore PHP to get the checkbox value. How to correctly implement PHP to obtain the checkbox value is still unfamiliar to beginners. Here we will introduce you in detail the relevant implementation methods, and hope to help you in how to correctly implement PHP to obtain the checkbox value is still unfamiliar to beginners. Here we will introduce the implementation methods in detail and hope to help you.

  1. < InputType = "checkbox" name = "weeks []"
    Id = "weeks" value = 1>
  2. < InputType = "checkbox" name = "weeks []"
    Id = "weeks" value = 2>
  3. < InputType = "checkbox" name = "weeks []"
    Id = "weeks" value = 3>


The brackets after weeks cannot be left blank. Otherwise, you can only get the last value when using PHP. PHP will handle the problem as follows:

Method 1 for PHP to obtain the checkbox value:

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

Method 2 for PHP to obtain the checkbox value:

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

Forms are frequently used, and check boxes are frequently used. But in PHP, unlike other scripting languages, the name of the check box must be followed by [], and then obtained through 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 = ""> Football <br>
  13. <Input type = "checkbox"
    Name = "t1 []" value = "table tennis"> Table Tennis <br>
  14. <Input type = "checkbox"
    Name = "t1 []" value = "volleyball"> Volleyball <br>
  15. <Input type = "submit">
  16. </Form>

The above two methods are described for PHP to get the checkbox value.


Bytes. Here we will introduce the implementation methods in detail, and hope to help you...

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.