1

I want to add rows dynamically in a HTML table and the number of the column should be fixed. After adding rows I want to input some text values in each row and column. After entering the text values I want to store it in a database table. How to do that? Here is my code

    function addRow() {
        var root = document.getElementById('mytab').getElementsByTagName('tbody')[0];
        var rows = root.getElementsByTagName('tr');
        var clone = cloneEl(rows[rows.length - 1]);
        root.appendChild(clone);
    }
    function addColumn() {
        var rows = document.getElementById('mytab').getElementsByTagName('tr'), i = 0, r, c, clone;
        while (r = rows[i++]) {
            c = r.getElementsByTagName('td');
            clone = cloneEl(c[c.length - 1]);
            c[0].parentNode.appendChild(clone);
        }
    }
    function cloneEl(el) {
        var clo = el.cloneNode(true);
        return clo;
    }
#mytab td {
  width: 100px;
  height: 20px;
  background: #cccccc;
}
<html>
<head>
    <title>Untitled Document</title>
</head>
<body>
<form action="">
    <input type="button" value="Add a Row" onclick="addRow()">
    <input type="button" value="Add a Column" onclick="addColumn()">
</form>
<br>

<table id="mytab" border="1" cellspacing="0" cellpadding="0">
    <tr>
        <td><input type="text" name="enter1"></td>
        <td><input type="text" name="enter2"></td>
    </tr>

</table>
</body>
</html> 

In this code, the problem is when I enter some text and after that, I add rows or column the same text appears in the newly added rows and column. But I want to enter different text in different boxes and I want the no of the column should be fixed to say no of the column should be 5.

1 Answers1

3

Updated

Hi, you have to clear input values after cloning elements, as below. cleanUpInputs(clone); will check cloned DOM and will remove input values. Check the code below.

function addRow() {
  var root = document.getElementById('mytab').getElementsByTagName('tbody')[0];
  var rows = root.getElementsByTagName('tr');
  var clone = cloneEl(rows[rows.length - 1]);
  cleanUpInputs(clone);
  root.appendChild(clone);
}
function addColumn() {
  var rows = document.getElementById('mytab').getElementsByTagName('tr'), i = 0, r, c, clone;
    while (r = rows[i++]) {
      c = r.getElementsByTagName('td');
      clone = cloneEl(c[c.length - 1]);
      cleanUpInputs(clone);
      c[0].parentNode.appendChild(clone);
    }
}
function cloneEl(el) {
  var clo = el.cloneNode(true);
  return clo;
}

function cleanUpInputs(obj) {
  for (var i = 0; n = obj.childNodes[i]; ++i) {
    if (n.childNodes && n.tagName != 'INPUT') {
      cleanUpInputs(n);
    } else if (n.tagName == 'INPUT' && n.type == 'text') {
      n.value = '';
    }
  }  
}
#mytab td {
width: 100px;
height: 20px;
background: #cccccc;
}
<html>
<head>
    <title>Untitled Document</title>
</head>
<body>
<form action="">
    <input type="button" value="Add a Row" onclick="addRow()">
    <input type="button" value="Add a Column" onclick="addColumn()">
</form>
<br>

<table id="mytab" border="1" cellspacing="0" cellpadding="0">
    <tr>
        <td><input type="text" name="enter1"></td>
        <td><input type="text" name="enter2"></td>
    </tr>

</table>
</body>
</html>
Community
  • 1
  • 1
  • Can these input values store in database using php? – Debasish Choudhury May 11 '17 at 22:44
  • @DebasishChoudhury for that you'd have to move the `` to after the `` – developerwjk May 11 '17 at 23:01
  • If you want to create a column in a database, first of all, you have to create a structure for it. Adding new column into database table you can do with MySql PDO driver [Add a new column to an existing table in MySql using PHP with PDO](http://stackoverflow.com/questions/20009987/add-a-new-column-to-an-existing-table-in-mysql-using-php-with-pdo) . Also, you must use [Send POST data using XMLHttpRequest](http://stackoverflow.com/questions/9713058/send-post-data-using-xmlhttprequest). – Teymur Mardaliyer Lennon May 11 '17 at 23:01
  • But I prefer to you using [jQuery+Ajax Live Edit Tables Plugins](http://www.webslesson.info/2016/02/live-table-add-edit-delete-using-ajax-jquery-in-php-mysql.html) – Teymur Mardaliyer Lennon May 11 '17 at 23:02