简体   繁体   English

将复选框值存储在数组中

[英]Storing checkbox values in an array

I have a set of 3 checkboxes that ask the user for input. 我有一组3个复选框,要求用户输入。 They are constructed like this: 它们的构造如下:

<span class="playback-format-checkbox-holder">
    <span><b>Playback Format</span><br />
        <label for="dvd">
          <input type="checkbox" name="playback_format[]" value="dvd"  id="dvd" />
        DVD
        </label>
        <label for="bluray">
          <input type="checkbox" name="playback_format[]" value="bluray"  id="bluray" />
        Bluray
        </label>
        <label for="3d">
          <input type="checkbox" name="playback_format[]" value="3d" id="3d"  />
        3D
        </label>
</span>

and I am posting the results like so 我这样发布结果

<?php
   $new = array();

   $playback_formats = $_POST['playback_format'];

   if ( isset($playback_formats) ) {
     $new['playback_format'] = $playback_formats;           
   }
?>

If I select two values, only one is returned. 如果选择两个值,则仅返回一个。 For example, if i select bluray and 3d, the array only stores bluray. 例如,如果我选择Bluray和3D,则阵列仅存储Bluray。 If I check dvd and 3d only dvd is stored. 如果我检查dvd和3d,则仅存储dvd。 So only the first selection is being stored in the playback_format array. 因此,只有第一个选择存储在playback_format数组中。

What have I done wrong? 我做错了什么?

If I check one checkbox in each row it returns the correct values. 如果我在每一行中选中一个复选框,它将返回正确的值。 the playback_format[] array is not storing as an array. playback_format []数组未存储为数组。 Why? 为什么?

If I check two boxes in the first row, and leave the second row blank the first row gets the first selection and the second row gets the second. 如果我选中第一行中的两个框,并将第二行留空,则第一行将获得第一个选择,第二行将获得第二个选择。

I have no idea what is going on. 我不知道发生了什么。 This seems like it shouldn't be this difficult... 看来应该不难...


This is when I check DVD and Bluray on the first set of checkboxes, and leave the second set of checkboxes blank. 这是当我在第一组复选框上选中DVD和Bluray并将第二组复选框留为空白时。

Array ( [0] => Array ( [0] => Array ( [select] =>
Testing Title #1

[url] =>
Description #1

[playback_format] => Array ( [0] => bluray ) ) [1] => Array ( [select] =>
Testing Title #2

[url] =>
Description #2

[playback_format] => Array ( [0] => dvd ) ) ) )
testing a new list

As you can see, its not storing in the correct array. 如您所见,它没有存储在正确的数组中。 The second checkbox value has been stored in the second array for whatever reason, and not in the first row playback_format array. 无论出于何种原因,第二个复选框值都已存储在第二个数组中,而不是存储在第一行的playback_format数组中。

This is driving me crazy. 这真让我抓狂。

您只想尝试,我认为那会更好

foreach ($playback_format as $3d){}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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