problem with this code is, it is printing only first element,how can i print all elements which are added using addrow()

<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 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>



   <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">
            <form method="post">  <tbody>

            <tr>
            <td><input type="checkbox" name="chk[]"></td>
            <td> 1 </td>
            <td> <input type="text" name="txtbox[]"> </td>


        </tr>


    </tbody>
    </table>


    <input type="submit" value="submit">
    </form>
    <?php

$txtbox = $_POST['txtbox'];


$N = count($txtbox);
    for($i=0; $i <= $N; $i++)
    {
      echo $txtbox[$i];
      echo "<br>";
    }

?>

I think you can use $j instead of $i in php

<script language="javascript">function addRow(tableID){var table=document.getElementById(tableID);var rowCount=table.rows.length;var row=table.insertRow(rowCount);var colCount=table.rows[0].cells.length;for(var i=0;i<colCount;i++){var newcell=row.insertCell(i);newcell.innerHTML=table.rows[0].cells[i].innerHTML;switch(newcell.childNodes[0].type){case"text":newcell.childNodes[0].value="";break;case"checkbox":newcell.childNodes[0].checked=false;break;case"select-one":newcell.childNodes[0].selectedIndex=0;break;}}}
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){if(rowCount<=1){alert("Cannot delete all the rows.");break;}
table.deleteRow(i);rowCount--;i--;}}}catch(e){alert(e);}}</script>

 <form method="post">

    <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">
        <tbody><tr>
            <td><input type="checkbox" name="chk[]"></td>
            <td><input type="text" name="txt[]"></td>
        </tr>

    </tbody></table>
    <br><br>
    <input type="submit" name="submit" value="submit">
</form>  

<?php
        if(isset($_POST['submit']) && $_POST['submit'] == 'submit')
        {

            $txtbox = $_POST['txt'];

            echo 'limit Count - '. $limit = count($txtbox).'<br>';

            for($j=0;$j<$limit;$j++)
            {
                echo $txtbox[$j].'<br>';

            }


        }


?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.