简体   繁体   中英

Get the checkbox value php

Good day sir. Please check my script first.

    <div class="form-group">
       <label class="btn control-label col-sm-2" for="email">Developer</label>
       <label class="btn btn-info">
       <input name='pilihan[]' value='1' type="checkbox" autocomplete="off"> Konfigurasi Menu                                           <span class="glyphicon glyphicon-ok"></span></label>
       <label class="btn btn-info">
       <input name='pilihan[]' value='2' type="checkbox" autocomplete="off"> Konfigurasi User                                           <span class="glyphicon glyphicon-ok"></span>
       </label>
       <label class="btn btn-info">
       <input name='pilihan[]' value='16' type="checkbox" autocomplete="off"> User Akses                                            <span class="glyphicon glyphicon-ok"></span>
       </label>
    </div>
    <div class="form-group">
       <label class="btn control-label col-sm-2" for="email">Admin</label>
       <label class="btn btn-info">
       <input name='pilihan[]' value='1' type="checkbox" autocomplete="off"> Konfigurasi Menu                                           <span class="glyphicon glyphicon-ok"></span>
       </label>
       <label class="btn btn-info">
       <input name='pilihan[]' value='2' type="checkbox" autocomplete="off"> Konfigurasi User                                           <span class="glyphicon glyphicon-ok"></span>
       </label>
       <label class="btn btn-info">
       <input name='pilihan[]' value='16' type="checkbox" autocomplete="off"> User Akses                                            <span class="glyphicon glyphicon-ok"></span>
       </label>
    </div>
    <div class="form-group">
       <label class="btn control-label col-sm-2" for="email">Outlet</label>
       <label class="btn btn-info">
       <input name='pilihan[]' value='1' type="checkbox" autocomplete="off"> Konfigurasi Menu                                           <span class="glyphicon glyphicon-ok"></span>
       </label>
       <label class="btn btn-info">
       <input name='pilihan[]' value='2' type="checkbox" autocomplete="off"> Konfigurasi User                                           <span class="glyphicon glyphicon-ok"></span>
       </label>
       <label class="btn btn-info">
       <input name='pilihan[]' value='16' type="checkbox" autocomplete="off"> User Akses                                            <span class="glyphicon glyphicon-ok"></span>
       </label>
    </div>

So what i want to is save the checkbox value. For now what i can do is

 [pilihan] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 16
        )

I can get the value but the value are free .

As you can see from my form above there are developer , admin & Outlet .

My question is, how to set the value for developer , admin & Outlet .

As you can see inside your html markup , you are creating the inputs for all of the roles with same name attribute , which leads to creating a one array with all of the selected values. In your case what you should do is you can create pilihan[] but additionally add the keys for specific roles for example:

developer:

 name="pilihan['developer'][]"

admin:

 name="pilihan['admin'][]"

Outlet:

 name="pilihan['outlet'][]"

the above structure will give you a following result

Array
(
    [developer] => Array
        (
            [0] => 2
            [1] => 3
            [2] => 16
        )

    [admin] => Array
        (
            [0] => 2
            [1] => 3
            [2] => 16
        )

    [outlet] => Array
        (
            [0] => 2
            [1] => 3
            [2] => 16
        )

)

So next what you can do is you can pick the specific array keys (developer, admin or outlet) and put them inside a foreach loop and get their values. example:

// this below will print all the roles's values
$data = $_POST['checkbox'];

foreach($data as $roles){
    print_r($roles);
}

for accessing specific role values :

$data = $_POST['checkbox'];
// this will print each single values from the array of checkbox but with key role `developer`    
foreach($data['developer'] as $values){
    print_r($values);
}

you can convert the checkbox values to array in foreach and insert

foreach($_POST['pilihan'] as $value) {
       print_r($value);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM