如何将动态添加的文本框插入到 MySQL 数据库中 [英] How to insert dynamically added text boxes into MySQL database

查看:99
本文介绍了如何将动态添加的文本框插入到 MySQL 数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I used Javascript to dynamically add textbox in order for the user to add more items in one process. Here's the code that add 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>


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


When the user clicks the "Add Text" button, one set (four textboxes) will appear. My problem is even if the user clicks and inputs data into those textbox, it will only insert one set of data (which was the last set of input). Its because of the "name" of the textbox are the same. How can I insert those multiple data? Or how can I set a unique name for the added textbox in order to insert them into the database?

推荐答案

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

You'll want to change the names with a [] appended to it. This will pass an array to the PHP on form submit.

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

要获取 PHP 中的值,

To get the values in PHP,

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

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

You will need to implement a loop to iterate through the values.

这篇关于如何将动态添加的文本框插入到 MySQL 数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆