0

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.

2
  • You wana keep pilihan[] as it is? Commented Jun 21, 2016 at 7:29
  • Well. that just a test for me. So, i won't Commented Jun 21, 2016 at 7:38

2 Answers 2

1

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

@YVS1102 Glad it helped , enjoy.
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.