akashaggarwaal
akashaggarwaal

Reputation: 189

counting the rows in html table using php

I have got a dynamic HTML table that allows the user to add rows on click of a button. The data from each row needs to be inserted in MySQL table for which i use the PHP script. Now since each time the number of rows in the HTML table may be different, how do I deal with this in PHP. One way is to count the number of rows in HTML table (which I found somewhere to be done like this)

$rows = $table->find('#studenttable'); /*line 4 of my code*/
$count = count($rows);                 /*studenttable is the id of my table*/
echo $count;

and then run a for loop for inserting the data for each row. But that is giving a fatal error.

Notice: Undefined variable: table in savedata.php on line 4

Fatal error: Call to a member function find() on a non-object in savedata.php on line 4

The other way round may be to use a foreach loop which I am completely not getting how to implement in this situation.

This code dynamically adds new rows

var count=2;

function addRow()
{


var table=document.getElementById("studenttable");
var row=table.insertRow(-1);
var cell15=row.insertCell(14);

var ipt8 = document.createElement('input');
    ipt8.setAttribute("type", "hidden");
    ipt8.name = "htmlrow[]";      
    ipt8.value = count;        
    cell15.appendChild(ipt8);
count++;
}

PHP file to get the number of rows

<?php
$arr = $_POST['htmlrow'];
foreach ($arr as $val) {
   $count= $val;
echo $count;

}
?>

still not getting result.

Upvotes: 4

Views: 6796

Answers (2)

Jack
Jack

Reputation: 446

in table.php

<script>
function addRow()
{
    var table = document.getElementById("studentsTable");
    var row = table.insertRow(-1);
    var cell = row.insertCell(0);
    //get the current count of rows
    //-1 for the head row another -1 to index from 0
    var last_row_index = table.getElementsByTagName("tr").length-2; 

    cell.innerHTML = '<input name="name_'+ last_row_index +'" type="text" />';
}

<body>

<form action="update.php" method="post">
    <table id="studentsTable">
    <tr>
        <th>Name</th>
    </tr>
    <tr>
        <td><input name="name_0" type="text" /></td>
    </tr>
    </table>
    <input name="submit" type="submit" value="Submit" />
    <br>
</form>
<button onclick="addRow()">Add New Row</button>
</body>

in update.php

<?php

foreach($_POST as $name => $value)
{
    if(substr($name, 0, 5) == 'name_')
    {
        echo $name .' : '. $value .'<br>' ;
    }
}

?>

the output will be something like:

name_0 : Jane

name_1 : Bob

name_2 : Sam

.

.

As many as the user added in table.php

You can of course put them in an array, insert into MySQL....etc.

Upvotes: 0

Andrei B&#226;rsan
Andrei B&#226;rsan

Reputation: 3523

You cannot directly access HTML elements in PHP.

My suggestion would be altering the client-side code so that whenever a row gets added to the table, an <input type="hidden" value="value-of-that-row" name="htmlrow[]"/> gets added to the form.

This way, when submitting the form (you have the whole thing wrapped inside a form, right?) you can access the generated table rows in the PHP handling the form using $_POST['htmlrow'], which will now contain an array of the user data.

Upvotes: 1

Related Questions