EHerman
EHerman

Reputation: 1903

Passing checkbox values into an array?

I have set up a group of checkboxes. They are dynamic, so the number of checkboxes will be different dependent on the person using the site. The structure of how the checkboxes are created is:

<label for="plabackformat-holder-label">Format</label>
  <div class=" playbackformat-holder-<?php echo $num; ?> playbackformat-holder">
    <div class="playback-format-radio-buttons">

    <label for="notset-<?php echo $num; ?>">
        <input type="checkbox" class="playbackformat-holder-radiobutton" value="notset" name="playback_format[<?php echo $second_num; ?>]" id="notset-<?php echo $num; ?>" <?php if($field['playback_format'] == 'notset') { echo 'checked'; } ?>>None
    </label>

    <label for="dvd-<?php echo $num; ?>">
        <input type="checkbox" class="playbackformat-holder-radiobutton" value="dvd" name="playback_format[<?php echo $second_num; ?>]" id="dvd-<?php echo $num; ?>" <?php if($field['playback_format'] == 'dvd') { echo 'checked'; } ?>>DVD
    </label>

    <label for="bluray-<?php echo $num; ?>">
       <input type="checkbox" class="playbackformat-holder-radiobutton" value="bluray" name="playback_format[<?php echo $second_num; ?>]" id="bluray-<?php echo $num; ?>" <?php if($field['playback_format'] == 'bluray') { echo 'checked'; } ?>>Bluray
    </label>    

    <label for="3d-<?php echo $num; ?>">
       <input type="checkbox" class="playbackformat-holder-radiobutton" value="3d" name="playback_format[<?php echo $second_num; ?>]" id="3d-<?php echo $num; ?>" <?php if($field['playback_format'] == '3d') { echo 'checked'; } ?>>3d
    </label><br />

 </div>                     
</div>

My save function is:

$new = array();

for ( $i = 0; $i < $count; $i++ ) {

    $new[$i]['playback_format'] = $playbackFormats[$i];

    }

I've been reading up on this issue and it seems its because my input fields do not contain unique names. I'm trying to store the data into an array, so it would be ['playback_format'] => dvd,3d,bluray or something similar.

Right now its only storing the last checked value. Is there a way I can use a forloop or something to iterate over the checked values and push them into my array??

Upvotes: 0

Views: 581

Answers (3)

Portekoi
Portekoi

Reputation: 1157

Checkboxes have all the same name in your example. Name it differently like :

<label for="plabackformat-holder-label">Format</label>
  <div class=" playbackformat-holder-<?php echo $num; ?> playbackformat-holder">
    <div class="playback-format-radio-buttons">

    <label for="notset-<?php echo $num; ?>">
        <input type="checkbox" class="playbackformat-holder-radiobutton" value="notset" name="playback_format_notset" id="notset-<?php echo $num; ?>" <?php if($field['playback_format_notset'] == 'notset') { echo 'checked'; } ?>>None
    </label>

    <label for="dvd-<?php echo $num; ?>">
        <input type="checkbox" class="playbackformat-holder-radiobutton" value="dvd" name="playback_format_dvd" id="dvd-<?php echo $num; ?>" <?php if($field['playback_format_dvd'] == 'dvd') { echo 'checked'; } ?>>DVD
    </label>

    <label for="bluray-<?php echo $num; ?>">
       <input type="checkbox" class="playbackformat-holder-radiobutton" value="bluray" name="playback_format_bluray" id="bluray-<?php echo $num; ?>" <?php if($field['playback_format_bluray'] == 'bluray') { echo 'checked'; } ?>>Bluray
    </label>    

    <label for="3d-<?php echo $num; ?>">
       <input type="checkbox" class="playbackformat-holder-radiobutton" value="3d" name="playback_format_3d" id="3d-<?php echo $num; ?>" <?php if($field['playback_format_3d'] == '3d') { echo 'checked'; } ?>>3d
    </label><br />

 </div>                     
</div>

And Try this in PHP :

//WHen you want to see what is send in Post :
var_dump($_POST);

//So, for get result :
$tab = array();
foreach($_POST as $key =>$value){
    $tab[$key] = $value;
    //Display it
    echo $key . "=" . $value;
}

Upvotes: 1

Tommy G
Tommy G

Reputation: 93

You can just get rid of the "$second_num" in each <input name="playback_format[]"/> html tag. This will put everything into an array for you once you submit the form. You can check this by adding this line to the page as a test.

<?php print_r($_REQUEST['playback_format']); ?>

Generally, You want to avoid any loop if they aren't required.

Hope that helps with what you are doing.

Upvotes: 1

Mark Parnell
Mark Parnell

Reputation: 9200

What is $second_num? Does it need to be a part of the input name?

You can get PHP to recognise the submitted values as an array if you do it this way:

<input name="playback_format[<?php echo $second_num; ?>][]">

Or if you don't need $second_num as part of the name, just:

<input name="playback_format[]">

$_POST['playback_format'] will then be an array containing all the selected options.

There is a section in the PHP docs specifically about this behaviour.

Upvotes: 1

Related Questions