When I wrote php today, I found that $ _ POST ["arr"] could not obtain the arr array parameter. Record it.
For example, you need to submit the following forms:
The code is as follows: |
Copy code |
<Input type = "checkbox" name = "arr" value = ""/> <Input type = "checkbox" name = "arr" value = ""/> <Input type = "checkbox" name = "arr" value = ""/> <Input type = "checkbox" name = "arr" value = ""/> |
Use$ _ POST ["arr"]Only the value of the last selected check box can be obtained. To obtain the value of all selected check boxes, you need to modify the form to the following:
The code is as follows: |
Copy code |
<Input type = "checkbox" name = "arr []" value = ""/> <Input type = "checkbox" name = "arr []" value = ""/> <Input type = "checkbox" name = "arr []" value = ""/> <Input type = "checkbox" name = "arr []" value = ""/> |
In this way, you can use$ _ POST ["arr"]Obtain all the selected checkbox values. How can we parse the obtained values.
Method 1
The code is as follows: |
Copy code |
<Form action = "test1.php" method = "post"> <? For ($ I = 0; $ I <10; $ I ++ ){ ?> <Input type = "checkbox" name ="Interests [] (cannot remove [])"Value =" <? = $ I?> "> Test <? = $ I?> <Br> <? } ?> <Input type = "submit"> </Form> Test1.php <? Php Foreach ($ _ POST as $ key => $ val ){ If (is_array ($ val )){ Foreach ($ val as $ v2 ){ Echo "$ v2 <br> "; } } } |
?>
Second usage
Test3.php
The code is as follows: |
Copy code |
<? Php If (isset ($ _ POST ['submit ']) { $ Users = $ _ POST ['user']; Foreach ($ users as $ key => $ val ){ Echo 'user', $ key, '=', $ val, '<br/> '; } } ?> <Form method = "post"> Zhangsan <input type = "text" name ="User [zhangsan]"Value =" 0 "/> <br/> Lisi <input type = "text" name ="User [lisi]"Value =" 1 "/> <br/> Wangwu <input type = "text" name ="User [wangwu]"Value =" 2 "/> <br/> Zhaoliu <input type = "text" name ="User [zhaoliu]"Value =" 3 "/> <br/> <Input type = "submit" name = "submit" value = "submit"/> </Form> |