Php retrieves an instance of the checkbox value of the check box. 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 into characters using implode to store the checkbox in html as an array, if we want to obtain the checkbox value selected by the user, we can use traversal or directly convert the array into characters using implode for 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
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: |
|
Foreach ($ _ POST ["chk"] as $ I => $) {Echo $ I. "=>". $ ." ";} // Or use $ _ POST ["chk"] [0] Separately ?> |
Result:
0 => 1
1 => 2
2 => 3
Example 2 use explode
The code is as follows: |
|
'Mandarin Chinese ', '11' => 'Japanese', '10' => 'Korean ', '9' => 'Spanish ', '8' => 'Russian ', '7' => 'German', '6' => 'French ', '5' => 'INC ', '4' => 'Minority language ', '3' => 'Chinese Minnan cout', '2' => 'Chinese Cantonese ', '12' => 'languages of other countries ',); $ lang = $ _ POST ['Lang']; if (is_array ($ lang) {$ cbsports ='
'; Foreach ($ CACHE ['Lang'] as $ key => $ val) {$ cbsports. ='
- '. $ Val .'
';} $ Cbsports. ='
'; 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: |
|
If (! Emptyempty ($ lang) {$ expr = join (",", $ lang );} |
Then, the original array can be regenerated.
The code is as follows: |
|
$ 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:
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:
II. server processing code
The code is as follows: |
|
/** * 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! '); } } |
...