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.
<table id="studenttable"> ... </table>is the concerned tableI'm using the "Simple php DOM Parser".... You either need to go to that website (simplehtmldom.sourceforge.net), download the script, and read the documentation (simplehtmldom.sourceforge.net/manual.htm). Or you can learn about the built-in DOMDocument way - php.net/manual/en/class.domdocument.php