4

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.

13
  • 2
    You answered your own question twice in the question, what/why are you asking us? Commented Jul 5, 2013 at 22:29
  • isn't the user submitting a form with the data he inserted ?? Commented Jul 5, 2013 at 22:30
  • I have made some edits to the question. The problem is that on submitting the the PHP file is returning a fatal error instead of printing the number of rows in the table. Commented Jul 5, 2013 at 22:35
  • <table id="studenttable"> ... </table> is the concerned table Commented Jul 5, 2013 at 22:37
  • 1
    You missed the first line from that link I'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 Commented Jul 6, 2013 at 5:09

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

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.