PHP使用数组[]从输入获取后期数据

时间:2016-12-11 13:49:47

标签: php arrays post

我正在创建一个用户可以动态添加额外字段的表单,每次创建新输入时,都会将其命名为accountemails [type],每个组的类型都不同。

我想发布此表单并检索按类型

分组的数据

例如,如果我有2个组 - group1和group2,我的输入将被命名为:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE HTML>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>JS Drum Kit</title>
  <link rel="stylesheet" href="styles.css">
</head>

<body>
  <div class="keys">
    <div data-key="65" class="key">
      <kbd>A</kbd>
      <span class="sound">clap</span>
    </div>
    <div data-key="83" class="key">
      <kbd>S</kbd>
      <span class="sound">hihat</span>
    </div>
    <div data-key="68" class="key">
      <kbd>D</kbd>
      <span class="sound">kick</span>
    </div>
    <div data-key="70" class="key">
      <kbd>F</kbd>
      <span class="sound">openhat</span>
    </div>
    <div data-key="71" class="key">
      <kbd>G</kbd>
      <span class="sound">boom</span>
    </div>
    <div data-key="72" class="key">
      <kbd>H</kbd>
      <span class="sound">ride</span>
    </div>
    <div data-key="74" class="key">
      <kbd>J</kbd>
      <span class="sound">snare</span>
    </div>
    <div data-key="75" class="key">
      <kbd>K</kbd>
      <span class="sound">tom</span>
    </div>
    <div data-key="76" class="key">
      <kbd>L</kbd>
      <span class="sound">tink</span>
    </div>

  </div>

  <audio data-key='65' src="sounds/clap.wav"></audio>
  <audio data-key="83" src="sounds/hihat.wav"></audio>
  <audio data-key="68" src="sounds/kick.wav"></audio>
  <audio data-key="70" src="sounds/openhat.wav"></audio>
  <audio data-key="71" src="sounds/boom.wav"></audio>
  <audio data-key="72" src="sounds/ride.wav"></audio>
  <audio data-key="74" src="sounds/snare.wav"></audio>
  <audio data-key="75" src="sounds/tom.wav"></audio>
  <audio data-key="76" src="sounds/tink.wav"></audio>


</body>

</html>
<script>
  $(document).ready(function() {
    function removeTransition(e) {
      if (e.propertyName !== 'transform') return;
      e.target.classList.remove('playing');
    }


    var key;

    function playSound(e) {
      const audio = $(`audio[data-key="${e.keyCode}"]`);
      const key = $(`div[data-key="${e.keyCode}"]`);
      key.addClass('playing');
      if (audio.length == 0) return;
      audio[0].currentTime = 0;
      audio[0].play();
    }
    $('body').on("keydown", playSound);
    const keys = $("[class=key]");
    keys.each(function(key) {
      keys[key].addEventListener("transitionend", removeTransition);
    });


  });
</script>

可能有多个具有相同组名的

对于php中的帖子,我试过

name="accountemails[group1]"
name="accountemails[group2]"

我虽然可以使用,但我可以使用foreach($_POST["accountemails"] as $type => $email) { } 作为每个组,然后使用$type作为每个值,但只显示每个组的最后发布的输入值

2 个答案:

答案 0 :(得分:1)

如果要添加具有相同名称的多个表单输入,则需要在名称的最后一端添加双括号。

 name="accountemails[group1][]"
 name="accountemails[group2][]"

然后,如果用户为group1添加了额外字段,则每个不同组中的每个新accountemails都将添加到您可以在foreach循环中检索的数组中。

答案 1 :(得分:1)

要详细说明 Benoti的答案,听起来像是在添加新值时覆盖了$_POST['accountemails']数据。将group数据添加到数组中。

$_POST['accountemails']['group1'] [] = "Group 1 - 1"; //Emulates html form: name = accountemails[group1][] with value of: Group 1 - 1
$_POST['accountemails']['group1'] [] = "Group 1 - 2"; //Emulates html form: name = accountemails[group1][] with value of: Group 1 - 2

$_POST['accountemails']['group2'] [] = "Group 2 - 1"; //Emulates html form: name = accountemails[group2][] with value of: Group 2 - 1
$_POST['accountemails']['group2'] [] = "Group 2 - 2"; //Emulates html form: name = accountemails[group2][] with value of: Group 2 - 2

foreach ($_POST as $v) {

    foreach ($v['group1'] as $email) {

        echo "Group 1 Email: " . $email;
        echo "<br>";
    }

    foreach ($v['group2'] as $email) {

        echo "Group 2 Email: " . $email;
        echo "<br>";     
    }
}

<强>输出

Group 1 Email: Group 1 - 1
Group 1 Email: Group 1 - 2
Group 2 Email: Group 2 - 1
Group 2 Email: Group 2 - 2