0

I am trying to POST the values of the dynamically added text boxes. How to assign names to these new textboxes[as array]? How to pass these array values to the .php file?

Below piece of code adds texbox dynmically

HTML Part:

<input type="checkbox" name="bugs" value="1">Check this if bugs are not available </br> </br>
<INPUT type="button" value="Add Row" onclick="addBugRow('bugTable')" />
<INPUT type="button" value="Delete Row" onclick="deleteBugRow('bugTable')" />

<TABLE id="bugTable" width="350px" border="1">
<TR>
            <TD></TD>
            <TD> No. </TD>
            <TD> PCR </TD> 
        <TD> Description </TD>

</TR>
<TR>
            <TD><INPUT type="checkbox" name="chk"/></TD>
            <TD> 1 </TD>
            <TD> <INPUT type="text" name="pcr[]" size="6"/> </TD> 
        <TD><textarea cols="75" rows="5" name="bugdata[]"> </textarea> </TD>

</TR>
</TABLE>

Java Script Part

function addBugRow(tableID) {

            var table = document.getElementById(tableID);

            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);

            var cell1 = row.insertCell(0);
            var element1 = document.createElement("input");
            element1.type = "checkbox";
            cell1.appendChild(element1);

            var cell2 = row.insertCell(1);
            cell2.innerHTML = rowCount;

            var cell3 = row.insertCell(2);
            var element2 = document.createElement("input");
            element2.type = "text";
        element2.name = "pcr[]"; // text field names in array
        element2.size = 6;
            cell3.appendChild(element2);

        }

on PHP side, I need to get values of the number of textboxes added and their values.

PHP Part:

foreach($_POST['pcr'] as $key=>$value)
 echo $key.' '.$value;
4

2 回答 2

2

为动态创建的元素添加名称:

var Input = document.createElement("input");     
Input.setAttribute("name", "ElementNameGoesHere");

至于将输入作为数组发布..是的,这是可能的:

输入命名:

<input name="array[]" />
<input name="array[]" />

PHP:

$_POST['array'][0] // First Input
$_POST['array'][1] // Second Input
于 2012-08-15T18:54:40.390 回答
1

如果您使用的是 jquery:

var count  = $("input[name*='newInput']").length;
var hidden = $("<input>").attr("type", "hidden")
                         .attr("name", "newInput[" + count + "]")
                         .val("hiddenStuff");
$("#someId").append(hidden);

这应该可以很好地发布到 PHP。只要您生成您知道在 PHP 端查找的可预测名称,您就应该能够轻松地将数据取回。

于 2012-08-15T18:57:32.467 回答