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.
- < InputType = "checkbox" name = "weeks []"
Id = "weeks" value = 1>
- < InputType = "checkbox" name = "weeks []"
Id = "weeks" value = 2>
- < 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:
- $weeks = $_POST['weeks'];
- for($i=0;$i< count
($weeks);$i++)
- echo $weeks[1]."< br>";
Method 2 for PHP to obtain the checkbox value:
- $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>
The above two methods are described for PHP to get the checkbox value.