【问题标题】:How to insert dynamically added text boxes into MySQL database如何将动态添加的文本框插入 MySQL 数据库
【发布时间】:2013-12-16 09:32:32
【问题描述】:

我使用Javascript 动态添加textbox,以便用户在一个进程中添加更多项目。这是添加textboxes的代码:

<script type="text/javascript">

    var quantity = document.getElementById('quantity');
    var item = document.getElementById('item');
    var serial = document.getElementById('serial');
    var property = document.getElementById('property');

    document.getElementById('add').onclick = function () {
        var input = document.createElement('input'),
        div = document.createElement('div');
        input.type = "text";
        input.setAttribute("name", "quant");
        div.appendChild(input);
        quantity.appendChild(div);

        var input2 = document.createElement('input'),
        div2 = document.createElement('div');
        input2.type = "text";
        input2.setAttribute("name", "items");
        div.appendChild(input2);
        item.appendChild(div);

        var input3 = document.createElement('input'),
        div3 = document.createElement('div');
        input3.type = "text";
        input3.setAttribute("name", "serno");
        div.appendChild(input3);
        serial.appendChild(div);

        var input4 = document.createElement('input'),
        div4 = document.createElement('div');
        input4.type = "text";
        input4.setAttribute("name", "proper");
        div.appendChild(input4);
        property.appendChild(div);

    };

</script>


当用户单击添加文本”按钮时,将出现一组(四个textboxes)。我的问题是即使用户单击并将数据输入到那些textbox 中,它也只会插入一组数据(这是最后一组输入)。这是因为textbox 的“名称”是相同的。如何插入这些多个数据?或者如何为添加的textbox 设置唯一名称以便将它们插入数据库?

【问题讨论】:

标签: javascript php sql-insert html


【解决方案1】:

您需要更改名称并附加一个[]。这将在表单提交时将数组传递给 PHP。

input.setAttribute("name", "quant[]");

要获取 PHP 中的值,

$quant_values = $_GET['quant']; // array of values 
$value_one = $quant_values[0];

您需要实现一个循环来遍历这些值。

【讨论】:

  • 我明白了。现在,我想知道如何获取数组 [] 的最大数量或 [] 中的最后一个数字是多少。所以我可以与for循环进行比较。谢谢
  • 现在好了。我用 end($quant_values); $compare=key($quant_values);获取最后一组数组。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 2013-12-18
  • 1970-01-01
  • 2015-03-10
  • 1970-01-01
  • 2021-10-29
  • 2012-05-01
相关资源
最近更新 更多