0

Hi everyone. I have this problem, my for each output is incorrect.
This is my form, I have a dynamic table. It think the problem is not here, but for the sake of more clear explanations.

f02560409ffa7053e6601d38d966fc3b

I am passing the values of that table to another page, I am POSTing it correctlt I guess because when I vard_dump() it, the array shows. and here is the output of vardump:

array(4) { ["txt"]=> array(1) { [0]=> string(5) "item1" } ["qty"]=> array(1) { [0]=> string(1) "2" } ["txtbox"]=> array(4) { [0]=> string(5) "item2" [1]=> string(1) "3" [2]=> string(5) "item3" [3]=> string(1) "4" } ["submit"]=> string(6) "Submit" }

But I don't know why my for each can't process it all. Here's my code for outputting them.

$chkbox = $_POST['chk'];
$txtbox = $_POST['txt'];
$qtys = $_POST['qty'];

foreach($txtbox as $a => $b)
  echo "$txtbox[$a]  -  $qtys[$a] <br />";

This code only outputs: **item1 - 2 **
which is the first row only.
How can I output all the data? Thanks in advance.

2
Contributors
3
Replies
23
Views
3 Years
Discussion Span
Last Post by cereal
0

Hi, can you show the form?

Looking at the array output, it seems most of the values are under the index key txtbox while only the first is under txt which is the one you're trying to loop:

array(4)
{
    ["txt"]=> array(1)
    {
        [0]=> string(5) "item1"
    }

    ["qty"]=> array(1)
    {
        [0]=> string(1) "2"
    }

    ["txtbox"]=> array(4)
    {
        [0]=> string(5) "item2"
        [1]=> string(1) "3"
        [2]=> string(5) "item3"
        [3]=> string(1) "4"
    }

    ["submit"]=> string(6) "Submit"
}

Edited by cereal

0

Thank you for giving time to my post. Here is my form.

<HTML>
<HEAD>
    <TITLE> Add/Remove dynamic rows in HTML table </TITLE>
    <SCRIPT language="javascript">
        function addRow(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";
            element1.name="chkbox[]";
            cell1.appendChild(element1);

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

            var cell2 = row.insertCell(1);
            var element3 = document.createElement("input");
            element3.type = "text";
            element3.name = "txtbox[]";
            cell2.appendChild(element3);

            var cell3 = row.insertCell(2);
            var element2 = document.createElement("input");
            element2.type = "text";
            element2.name = "txtbox[]";
            cell3.appendChild(element2);


        }

        function deleteRow(tableID) {
            try {
            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;

            for(var i=0; i<rowCount; i++) {
                var row = table.rows[i];
                var chkbox = row.cells[0].childNodes[0];
                if(null != chkbox && true == chkbox.checked) {
                    table.deleteRow(i);
                    rowCount--;
                    i--;
                }


            }
            }catch(e) {
                alert(e);
            }
        }

    </SCRIPT>
</HEAD>
<BODY>

    <form method="post" action="tables.php">
    <INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />

    <INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" />

    <TABLE id="dataTable" width="350px" border="1">

            <TR>
                <TD> </TD>
                <TD> NAME </TD>
                <TD> QTY </TD>
            </TR>

        <TR>
            <TD><INPUT type="checkbox" name="chk[]"/></TD>
            <TD><INPUT type="text" name="txt[]"/></TD>
            <TD> <INPUT type="text" name="qty[]"/> </TD>
        </TR>
    </TABLE>

    <input type="submit" name="submit" >

</form>

</BODY>
</HTML>
1

Ok, change your javascript to:

element1.name="chk[]";
element2.name="txt[]";
element3.name="qty[]";

that will generate this structure:

Array
(
    [chk] => Array
        (
            [0] => on
            [1] => on
            [2] => on
        )

    [txt] => Array
        (
            [0] => item 1
            [1] => item 2
            [2] => item 3
        )

    [qty] => Array
        (
            [0] => 17
            [1] => 34
            [2] => 23
        )

    [submit] => Submit
)

And it should work fine.

Votes + Comments
Thank you very much! It Works!
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.