Checkbox is stored in the form of an array in HTML. If we want to get the checkbox value selected by the user, we can use traversal or directly convert the array into characters using implode to store it.
PHP requires form elements with the same name to be named in array form and assigned value. Otherwise he cannot decompose.
Example 1: Using foreach to traverse checkbox
代码如下 | 复制代码 |
|
After submission, use $_POST['chk'] to access. The elements in the array are the values of the selected check boxes
PHP code:
The code is as follows | Copy code | ||||
{ echo $i."==>".$a." "; } //Or use $_POST["chk"][0] alone ?> |
结果:
0=>1
1=>2
2=>3
例2利用explode
代码如下 | 复制代码 | ||||
|
代码如下 | 复制代码 |
if(!emptyempty($lang)) { $expr = join(",", $lang); } |
代码如下 | 复制代码 |
if(!emptyempty($lang)) { $expr = join(",", $lang); } |
The original array can be regenerated after processing like this when displaying.
The code is as follows | Copy code | ||||
|
Note: Arrays can be stored directly using serialize() and unserialize(), but the fields must be of text type, which is not ideal.
Example 3: You need to delete multiple records at the same time, or modify multiple records at the same time
Key Points:
1. Add [] to the name in the front-end form, such as:
2. The backend accepts: $id = $_POST [id] [$i]; $i is the loop variable in for. When $i is the same, $_POST [id] [$i] and $_POST [name ] [$i] is a one-to-one correspondence;
1. Front-end form:
The code is as follows | Copy code |
|
二,服务端处理代码
代码如下
|
复制代码 | ||||
/** * * Modify status in batches */ public function updateList() { $result = false; for($i = 0; $i < count ( $_POST [id] ); $i ++) { $id = $_POST [id] [$i]; $check = $_POST [check] [$i]; $status = $_POST [status] [$i]; // 保存登录信息 $Repair = M ( 'Repair' );