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! '); } } |