Php uses $ _ POST to obtain the form name = [] array.

Source: Internet
Author: User

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>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.