title: PHP Save multiple checkbox values
body: <p>I'm making a theme options for Wordpress and I'm trying to create a multiple checkbox option in one settings field. However only one checkbox value is being saved. After some searching around I've realized that multiple check-boxes need to be saved differently then single check-boxes. But i can't seem to find a straight answer on how to do this.</p>
  
  <p>Here's my markup.</p>
  
  <pre><code>&lt;div class="checkbox-toggle"&gt;
     &lt;input id="show_date" type="checkbox" value="1" name="mbpanel_display_section_options[show_meta]" &lt;?php if(1 == $options['show_meta']) echo 'checked="checked"'; ?&gt; /&gt;
     &lt;label for="show_date"&gt;
        &lt;div&gt;
           &lt;span&gt;Off&lt;/span&gt;
           &lt;span&gt;On&lt;/span&gt;
        &lt;/div&gt;
     &lt;/label&gt;
     &lt;p class="input-description"&gt;Date&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="checkbox-toggle"&gt;
     &lt;input id="show_author" type="checkbox" value="2" name="mbpanel_display_section_options[show_meta]" &lt;?php if(2 == $options['show_meta']) echo 'checked="checked"'; ?&gt; /&gt;
     &lt;label for="show_author"&gt;
        &lt;div&gt;
           &lt;span&gt;Off&lt;/span&gt;
           &lt;span&gt;On&lt;/span&gt;
        &lt;/div&gt;
     &lt;/label&gt;
     &lt;p class="input-description"&gt;Author&lt;/p&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>That's how I've been doing it for single checkboxes. So my question is how can i save all of the check-boxes that get submitted and echo the checked value on them ? From what I've researched i need to save the values in an array and check if it's in the array but I'm not experienced with php so don't fully understand what I'm doing. Any help would be greatly appreciated.</p>
  
  <p>EDIT:</p>
  
  <p>I've tried applying the answers from the possible duplicate post. But those answers don't seem to work at all. After applying those answers neither checkboxes are saved. Maybe I'm applying it in the wrong way ?</p>
  
  <pre><code>    &lt;div class="checkbox-toggle"&gt;
          &lt;input id="show_date" type="checkbox" value="1" name="mbpanel_display_section_options[show_meta]" &lt;?php if(isset($_POST['mbpanel_display_section_options'])){ echo 'checked="checked"'; } ?&gt; /&gt;
          &lt;label for="show_date"&gt;&lt;div&gt;&lt;span&gt;Off&lt;/span&gt;&lt;span&gt;On&lt;/span&gt;&lt;/div&gt;&lt;/label&gt;
          &lt;p class="input-description"&gt;Date&lt;/p&gt;
      &lt;/div&gt;
      &lt;div class="checkbox-toggle"&gt;
          &lt;input id="show_author" type="checkbox" value="2" name="mbpanel_display_section_options[show_meta]" &lt;?php if(isset($_POST['mbpanel_display_section_options'])){ echo 'checked="checked"'; } ?&gt; /&gt;
          &lt;label for="show_author"&gt;&lt;div&gt;&lt;span&gt;Off&lt;/span&gt;&lt;span&gt;On&lt;/span&gt;&lt;/div&gt;&lt;/label&gt;
          &lt;p class="input-description"&gt;Author&lt;/p&gt;
      &lt;/div&gt;
  
  &lt;?php
      if(!empty($_POST['mbpanel_display_section_options'])) {
          foreach($_POST['mbpanel_display_section_options'] as $check) {
              echo $check;
         }
      }
  ?&gt;
  </code></pre>
  
  <p>Like i said i'm not experienced with php.</p>
  
  <p>EDIT:</p>
  
  <p>From what i understand i need to set the input name as</p>
  
  <pre><code>&lt;input name="The section on which this option will be displayed[ID used to identify the field]" /&gt;
  </code></pre>
  
  <p>So my full code is</p>
  
  <pre><code>//Initialize the Display Options page by registering the Sections, Fields, and Settings
  function mbpanel_initialize_display_options() {
  
  // If the display options don't exist, create them
  if( false == get_option( 'mbpanel_display_section_options' ) ) {  
  add_option( 'mbpanel_display_section_options' );
  } // end if
  
  // Register Display Options section
  add_settings_section(
      'display_settings_section_id',      // ID used to identify this section and with which to register options
      'Display Options',                  // Title to be displayed on the administration page
      'mbpanel_display_options_callback', // Callback used to render the description of the section
      'mbpanel_display_section_options'                           // Section on which to add this section of options
  );
  
  // Add Display Options section fields
  add_settings_field( 
      'show_meta',                      // ID used to identify the field throughout the theme
      'Meta',                           // The label to the left of the option interface element
      'mbpanel_toggle_meta_callback',   // The name of the function responsible for rendering the option interface
      'mbpanel_display_section_options',                          // The section on which this option will be displayed
      'display_settings_section_id'       // The name of the section to which this field belongs
  );
  
  // Register Display Options fields
  register_setting(
      'mbpanel_display_section_options',    // Group/Page this setting belongs to
      'mbpanel_display_section_options'     // ID of the field being registered
  );
  } // end mbpanel_initialize_display_options
  
  add_action('admin_init', 'mbpanel_initialize_display_options');
  
  /* ------------------------------------------------------------------------ *
   * Display Options Section Callbacks
   * ------------------------------------------------------------------------     */
  
  // Display Options section description
  function mbpanel_display_options_callback() {
  echo '&lt;p class="page-description"&gt;Select which areas of content you wish to display.&lt;/p&gt;';
  } // end mbpanel_display_options_callback
  
  /* ------------------------------------------------------------------------ *
   * Display Options Field Callbacks
   * ------------------------------------------------------------------------ */
  
  // Render the Display Options interface elements for toggling the visibility of the header element
  function mbpanel_toggle_meta_callback() {
  
  // First, we read the options collection
  $options = get_option('mbpanel_display_section_options');
  
  // Next, we update the name attribute to access this element's ID in the context of the display options array
  // We also access the show_header element of the options collection in the call to the checked() helper function
  ?&gt;
  &lt;div class="option-wrap"&gt;
      &lt;div class="checkbox-toggle"&gt;
          &lt;input id="show_date" type="checkbox" value="1" name="mbpanel_display_section_options[show_meta]" &lt;?php if(1 == $options['show_meta']) echo 'checked="checked"'; ?&gt; /&gt;
          &lt;label for="show_date"&gt;&lt;div&gt;&lt;span&gt;Off&lt;/span&gt;&lt;span&gt;On&lt;/span&gt;&lt;/div&gt;&lt;/label&gt;
          &lt;p class="input-description"&gt;Date&lt;/p&gt;
      &lt;/div&gt;
      &lt;div class="checkbox-toggle"&gt;
          &lt;input id="show_author" type="checkbox" value="2" name="mbpanel_display_section_options[show_meta]" &lt;?php if(2 == $options['show_meta']) echo 'checked="checked"'; ?&gt; /&gt;
          &lt;label for="show_author"&gt;&lt;div&gt;&lt;span&gt;Off&lt;/span&gt;&lt;span&gt;On&lt;/span&gt;&lt;/div&gt;&lt;/label&gt;
          &lt;p class="input-description"&gt;Author&lt;/p&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;p class="option-description"&gt;Choose which post meta info you wish to display.&lt;/p&gt;
  &lt;?php
  
  } // end mbpanel_toggle_meta_callback
  </code></pre>
  
  <p>Setting the input field like</p>
  
  <pre><code>&lt;input id="show_date" type="checkbox" value="1" name="mbpanel_display_section_options[show_meta][]" &lt;?php if(isset($_POST['mbpanel_display_section_options'])){ echo 'checked="checked"'; } ?&gt; /&gt;
  </code></pre>
  
  <p>or something like</p>
  
  <pre><code>&lt;input type='checkbox' name='myCB[]' value='cheese sauce'&gt;
  </code></pre>
  
  <p>does nothing nor does</p>
  
  <pre><code>&lt;?php
  if(!empty($_POST['mbpanel_display_section_options'])) {
      foreach($_POST['mbpanel_display_section_options'] as $check) {
          echo $check;
     }
  }
  ?&gt;
  </code></pre>
  
  <p>nor do i even understand what it's meant to do. Sorry if i'm being a pain here but i've spent all day trying to get this to work and everything i've tried hasn't worked, i can't seem to apply similar instances to this specific case. also when i read things like "make it an array" i have no idea what that means.</p>