PHP retrieves the checkbox Value

Source: Internet
Author: User
<Input type = "checkbox" name = "weeks []" id = "weeks" value = 1> <input type = "checkbox" name = "weeks []" id =" weeks "value = 2> <input type =" 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:

 
$ Weeks=$ _ Post['Dones'];For($ I= 0;$ I<Count($ Weeks);$ I++)Echo $ Weeks[$ I]. "<Br> ";

 

Method 2:

 
$ Array=$ This-> Request-> getparameter ("weeks []");$ Str=Implode(',',$ Array);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.

<? PHP  If (! Empty ( $ _ Post ["T1" ]) { $ Array = $ _ Post ["T1" ];  $ Size = Count ( $ Array  );  For ( $ I = 0; $ I < $ Size ; $ I ++ ){  Echo  $ Array [ $ I ]. "<Br>" ;}} ?> <Form method = post action = "" name = "form1"> <input type = "checkbox" name = "T1 []" value = "basketball"> basketball <br> <input type = "checkbox" name = "T1 []" value = ""> football <br> <input type = "checkbox" name = "T1 []" value =" table Tennis "> Table Tennis <br> <input type =" checkbox "name =" T1 [] "value =" Volleyball "> volleyball <br> <input type =" Submit "> </form>

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.