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;
6
  • Do you want to post the code that generates the textboxes? Commented Aug 15, 2012 at 18:52
  • Um, if they are part of the form, they should be sent when the form submits. Are you asking how to assign a name value to the <input type="text" /> elements? Commented Aug 15, 2012 at 18:54
  • How are the textboxes generated? Posting code would provide a more useful response.. Commented Aug 15, 2012 at 18:59
  • @andrewsi I need to post values of textboxes in a array. Commented Aug 16, 2012 at 2:46
  • @thatidiotguy I am trying to name the new textbox as new array. Commented Aug 16, 2012 at 2:48

2 Answers 2

2

Adding name to dynamically created element:

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

As for posting the inputs as an array.. Yes this is possible as so:

Input Naming:

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

PHP:

$_POST['array'][0] // First Input
$_POST['array'][1] // Second Input
Sign up to request clarification or add additional context in comments.

2 Comments

Can I name this as an array and POST the same array to PHP code.
Just tested it, will revise answer to include this. Please accept my answer if this works for you :D
1

If you're using jquery:

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

This should post just fine to PHP. As long as you generate predictable names that you know to look for on the PHP side, you should easily be able to get the data back out.

2 Comments

on PHP side, I need to get values of the number of textboxes added and their values. can I use array to name them and get their values.
For sure! There's no reason you couldn't use an array for the name so that you get an array created in PHP's $_POST global variable. You could even modify the code so it automatically added the "nth" item quite easily. I'll update my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.