2016-04-20 13 views
1

Ich mache ein Thema Optionen für Wordpress und ich versuche, eine mehrfache Kontrollkästchen Option in einem Einstellungsfeld zu erstellen. Es wird jedoch nur ein Kontrollkästchenwert gespeichert. Nach einigem Suchen habe ich festgestellt, dass mehrere Checkboxen anders als einzelne Checkboxen gespeichert werden müssen. Aber ich kann keine klare Antwort darauf finden, wie das geht.PHP Speichern Sie mehrere Kontrollkästchen Werte

Hier ist mein Markup.

<div class="checkbox-toggle"> 
    <input id="show_date" type="checkbox" value="1" name="mbpanel_display_section_options[show_meta]" <?php if(1 == $options['show_meta']) echo 'checked="checked"'; ?> /> 
    <label for="show_date"> 
     <div> 
     <span>Off</span> 
     <span>On</span> 
     </div> 
    </label> 
    <p class="input-description">Date</p> 
</div> 
<div class="checkbox-toggle"> 
    <input id="show_author" type="checkbox" value="2" name="mbpanel_display_section_options[show_meta]" <?php if(2 == $options['show_meta']) echo 'checked="checked"'; ?> /> 
    <label for="show_author"> 
     <div> 
     <span>Off</span> 
     <span>On</span> 
     </div> 
    </label> 
    <p class="input-description">Author</p> 
</div> 

So habe ich es für einzelne Checkboxen getan. Also meine Frage ist, wie kann ich alle Check-Boxen speichern, die eingereicht werden und den geprüften Wert auf ihnen Echo? Nach dem, was ich erforscht habe, muss ich die Werte in einem Array speichern und überprüfen, ob es in dem Array ist, aber ich habe keine Erfahrung mit PHP, also verstehe nicht ganz, was ich mache. Jede Hilfe würde sehr geschätzt werden.

EDIT:

Ich habe versucht, die Antworten aus dem möglichen doppelten Beitrag Anwendung. Aber diese Antworten scheinen überhaupt nicht zu funktionieren. Nach dem Anwenden dieser Antworten werden keine Kontrollkästchen gespeichert. Vielleicht wende ich es falsch an?

<div class="checkbox-toggle"> 
     <input id="show_date" type="checkbox" value="1" name="mbpanel_display_section_options[show_meta]" <?php if(isset($_POST['mbpanel_display_section_options'])){ echo 'checked="checked"'; } ?> /> 
     <label for="show_date"><div><span>Off</span><span>On</span></div></label> 
     <p class="input-description">Date</p> 
    </div> 
    <div class="checkbox-toggle"> 
     <input id="show_author" type="checkbox" value="2" name="mbpanel_display_section_options[show_meta]" <?php if(isset($_POST['mbpanel_display_section_options'])){ echo 'checked="checked"'; } ?> /> 
     <label for="show_author"><div><span>Off</span><span>On</span></div></label> 
     <p class="input-description">Author</p> 
    </div> 

<?php 
    if(!empty($_POST['mbpanel_display_section_options'])) { 
     foreach($_POST['mbpanel_display_section_options'] as $check) { 
      echo $check; 
     } 
    } 
?> 

Wie gesagt, ich habe keine Erfahrung mit PHP.

EDIT:

Von dem, was ich verstehe, ich brauche die Eingangsnamen als

<input name="The section on which this option will be displayed[ID used to identify the field]" /> 

So Code meines voller gesetzt ist

//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 '<p class="page-description">Select which areas of content you wish to display.</p>'; 
} // 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 
?> 
<div class="option-wrap"> 
    <div class="checkbox-toggle"> 
     <input id="show_date" type="checkbox" value="1" name="mbpanel_display_section_options[show_meta]" <?php if(1 == $options['show_meta']) echo 'checked="checked"'; ?> /> 
     <label for="show_date"><div><span>Off</span><span>On</span></div></label> 
     <p class="input-description">Date</p> 
    </div> 
    <div class="checkbox-toggle"> 
     <input id="show_author" type="checkbox" value="2" name="mbpanel_display_section_options[show_meta]" <?php if(2 == $options['show_meta']) echo 'checked="checked"'; ?> /> 
     <label for="show_author"><div><span>Off</span><span>On</span></div></label> 
     <p class="input-description">Author</p> 
    </div> 
</div> 
<p class="option-description">Choose which post meta info you wish to display.</p> 
<?php 

} // end mbpanel_toggle_meta_callback 

Einstellen des Eingabefeldes wie

<input id="show_date" type="checkbox" value="1" name="mbpanel_display_section_options[show_meta][]" <?php if(isset($_POST['mbpanel_display_section_options'])){ echo 'checked="checked"'; } ?> /> 

oder einige Ding wie

<input type='checkbox' name='myCB[]' value='cheese sauce'> 

tut nichts, noch tut

<?php 
if(!empty($_POST['mbpanel_display_section_options'])) { 
    foreach($_POST['mbpanel_display_section_options'] as $check) { 
     echo $check; 
    } 
} 
?> 

noch verstehe ich auch, was es zu tun, gemeint ist. Tut mir leid, wenn ich hier ein Problem habe, aber ich habe den ganzen Tag damit verbracht, dies zur Arbeit zu bringen, und alles, was ich versucht habe, hat nicht funktioniert, ich kann anscheinend nicht ähnliche Fälle auf diesen speziellen Fall anwenden. Auch wenn ich Dinge wie "mach es ein Array" lese, habe ich keine Ahnung was das bedeutet.

+1

Mögliches Duplikat von [Get $ \ _ POST aus mehreren Kontrollkästchen] (http://stackoverflow.com/questions/4997252/get-post-from-multiple-checkboxes) –

+0

Ändern Sie den Namen der Eingabe in /> – WhSol

Antwort

0

Sie benötigen ot Ihr Checkbox

<input type='checkbox' name='myCB[]' value='cheese sauce'>

Dann ein Array machen benennen Sie sie auf der anderen Seite von der $_POST['myCB'] Array bekommen kann (es ist ein Array).

+0

Aber wenn ich so etwas tue, dann funktioniert es nicht alle. Siehe meinen bearbeiteten Post. – Mark

Verwandte Themen