Checkbox is stored as an array in html. If we want to obtain the checkbox value selected by the user, we can use traversal or directly convert the array to implode for character storage.
Php requires that the form elements with the same name be named in the form of arrays and value is assigned. Otherwise, it cannot be broken down.
Example 1: Use foreach to traverse checkbox
| The Code is as follows: |
Copy code |
<Input type = checkbox name = "chk []" value = 1>
<Input type = checkbox name = "chk []" value = 2>
<Input type = checkbox name = "chk []" value = 3>
|
After submission, use $ _ POST ['chk'] for access. The element in the array is the value of the selected check box.
PHP code:
| The Code is as follows: |
Copy code |
|
<? Php
Foreach ($ _ POST ["chk"] as $ I => $)
{Echo $ I. "=>". $ a. "<br/> ";}
// Or use $ _ POST ["chk"] [0] separately
?> |
Result:
0 => 1
1 => 2
2 => 3
Example 2 use explode
| The Code is as follows: |
Copy code |
|
<Table> <form action = "index. php "method =" post "> <tr> <td> <ol> <li> <input type =" checkbox "name =" lang [] "id =" lang "value = "1"/> Mandarin </li> <input type = "checkbox" name = "lang []" id = "lang" value = "11 "/> Japanese </li> <input type = "checkbox" name = "lang []" id = "lang" value = "10"/> Korean </li> <li> <input type = "checkbox" name = "lang []" id = "lang" value = "9"/> Spanish </li> <input type = "checkbox" name = "lang []" id = "l Ang "value =" 8 "/> Russian </li> <input type =" checkbox "name =" lang [] "id =" lang "value =" 7 "/> German </li> <input type =" checkbox "name =" lang [] "id =" lang "value =" 6 "/> French </ li> <input type = "checkbox" name = "lang []" id = "lang" value = "5"/> English </li> <input type = "checkbox" name = "lang []" id = "lang" value = "4"/> Minority Languages </li> <input type =" checkbox "name =" lang [] "id =" lang "value =" 3 "/> Chinese Minnan language </li> <input ty Pe = "checkbox" name = "lang []" id = "lang" value = "2"/> Cantonese </li> <input type = "checkbox" name = "lang []" id = "lang" value = "12"/> other languages </li> </ol> </td> </tr> <tr> <td> <input name = "submit" type = "submit" value = "submit"/> </td> </tr> </form> </table> <? Php $ CACHE ['lang '] = array ('1' => 'mandarin Chinese', '11' => 'Japanese ', '10' => 'Korean ', '9' => 'Spanish ', '8' => 'Russian', '7' => 'German ', '6' => 'French ', '5' => 'English ', '4' => 'minority Language', '3' => 'Chinese Minnan Language', '2' => 'Chinese Cantonese ', '12' => 'languages of other countries ',); $ lang = $ _ POST ['lang']; if (is_array ($ lang )) {$ cbsports = '<ol>'; foreach ($ CACHE ['lang '] as $ key => $ val) {$ cbsports. = '<li> <input type = "checkbox" name = "lang []" id = "lang" value = "'. $ key. '"'. (in_array ($ key, $ lang )? 'Checked = "checked "':''). '/> '. $ val. '</li>';} $ cbsports. = '</ol>'; echo $ cbsports;} else {echo 'Check and submit';}?>
|
If you want to save the data to the database, store the $ expr value as follows.
| The Code is as follows: |
Copy code |
|
If (! Emptyempty ($ lang) {$ expr = join (",", $ lang );} |
Then, the original array can be regenerated.
| The Code is as follows: |
Copy code |
|
$ Lang = explode (',', $ row (lang )); |
Note: Arrays can be directly stored using serialize () and unserialize (). However, fields of the text type are not ideal.
Example 3: You must delete multiple or modify multiple records at the same time.
Key points:
1. Add [] to the name in the front-end form, for example, <input type = "hidden" name = "id []" value = "{$ vo. id}">
2. the backend accepts the following information: $ id =$ _ POST [id] [$ I]; $ I is the cyclic variable in, when $ I is the same, $ _ POST [id] [$ I] and $ _ POST [name] [$ I] can be matched one by one;
I. Front-end forms:
| The Code is as follows: |
Copy code |
|
<Form name = "form1" method = "post" action = "_ GROUP _/Repair/updateList">
For the selected operation: <input type = "submit" value = "save" class = "btn-primary"> <br>
<Table id = "checkList" class = "list table-hover">
<Tr>
<Th scope = "col"> <input id = "check" type = "checkbox"
Onclick = "checkAll ()"/> select all </th>
<Th scope = "col"> topic </th>
<Th scope = "col"> campus </th>
<Th scope = "col"> Room </th>
<Th scope = "col"> repairer </th>
<Th scope = "col"> contact number </th>
<Th scope = "col"> repair time </th>
<Th scope = "col"> review </th>
<Th scope = "col"> repair Report status </th>
</Tr>
<Volist name = "list" id = "vo">
<Tr>
<Td> <input type = "checkbox" name = "key" value = "{$ vo. id} "> {$ vo. id} <input type = "hidden" name = "id []" value = "{$ vo. id} "> </td>
<Td >{$ vo. topic} </td>
<Td >{$ vo. xiaoqu} </td>
<Td >{$ vo. room} </td>
<Td >{$ vo. pname} </td>
<Td >{$ vo. phone} </td>
<Td >{$ vo. create_time | date = 'Y-m-d H: I: s', ###} </td>
<Td> <select name = "check []"> <switch name = "vo. check">
<Case value = "0">
<Option value = "0" selected = "selected"> unreviewed </option>
<Option value = "1"> review </option>
</Case> <default/>
<Option value = "0"> not reviewed </option>
<Option value = "1" selected = "selected"> review </option>
</Switch> </select> </td>
<Td> <select name = "status []"> <switch name = "vo. status">
<Case value = "repaired">
<Option value = "not repaired"> not repaired </option>
<Option value = "task down"> task down </option>
<Option value = "repaired" selected = "selected"> repaired </option>
</Case> <case value = "task already exists">
<Option value = "not repaired"> not repaired </option>
<Option value = "task down" selected = "selected"> task down </option>
<Option value = "repaired"> repaired </option>
</Case> <default/>
<Option value = "not repaired" selected = "selected"> not repaired </option>
<Option value = "task down"> task down </option>
<Option value = "repaired"> repaired </option>
</Switch> </select> </td>
</Tr>
</Volist>
</Table>
</Form> |
Ii. server processing code
| The Code is as follows: |
Copy code |
|
<? Php
/**
* Batch modify status
*/
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];
// Save the logon information
$ Repair = M ('repair ');
$ Data ['id'] = $ id;
$ Data ['check'] = $ check;
$ Data ['status'] = $ status;
$ Result = $ Repair-> save ($ data );
}
If (false! ==$ Result ){
$ This-> success ('modification successful! ');
} Else {
$ This-> error ('modification failed! ');
}
} |