du1108 2012-05-09 14:49
浏览 127

HTML / PHP添加多个文本字段并将它们组合成1个字符串

I am creating a form where a user can input certain information. The default state looks as following;

<div class="options">
<input type="text" name="option[]" />
<input type="text" name="content[]" />
</div>

Now, by clicking a button ADD, I will add two new fields with the same name so the state is as follows;

    <div class="options">
    <input type="text" name="option[]" />
    <input type="text" name="content[]" />
    <input type="text" name="option[]" />
    <input type="text" name="content[]" />
    </div>

Then, I want PHP to eventually create a string as below;

valueOfFirstOption:valueOfFirstContent,valueOfSecondOption:valueOfSecondContent

And so on, and so on. Theoretically I should be able to add an infinite amount of input fields.

Is this possible what I am trying to achieve? Or should I be adding unique names to the input fields?

Any tips or hints how I should be going? Or somebody got a tutorial/example code for me?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • duandan1995 2012-05-09 15:07
    关注

    This would be a bit nicer if you could set indexes in the name when you add the extra elements.

    <div class="options">
      <input type="text" name="data[0][option]" />
      <input type="text" name="data[0][content]" />
      <input type="text" name="data[1][option]" />
      <input type="text" name="data[1][content]" />
    </div>
    <a href="#" onclick="addSet(); return false;">Add</a>
    <script>
        var addSet = function() {
            var container = document.getElementsByClassName('options')[0];
            var numInputs = container.getElementsByTagName('input');
            var numSets = numInputs/2;
            var nextIndex = numSets;
    
            var newOption = document.createElement('input');
            newOption.name = 'data[' + nextIndex + '][option]';
            container.appendChild(newOption);
    
            var newContent = document.createElement('input');
            newContent.name = 'data[' + nextIndex + '][content]';
            container.appendChild(newContent);
        }
    </script>
    

    Then when you receive the POST, you can just loop through data and process the duplets

    $pieces = array();
    
    foreach ($_POST['data'] as $set)
        $pieces = $set['option'] . ':' . $set['content'];
    }
    
    // or
    for ($i = 0; $i < count($_POST['data']); $i++) {
        $pieces[] = $_POST['data'][$i]['option'] . ':' . $_POST['data'][$i]['content'];
    }
    
    // then
    $compiledString = implode(",", $pieces);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?