Php: getting the checkbox value of the check box _ PHP Tutorial

Source: Internet
Author: User
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

The code is as follows:



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. ='
  1. '. $ Val .'
  2. ';} $ 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:

The code is as follows:


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

...

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.