Click here to Skip to main content
16,012,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add new rows into the table with data that is entered into the form.

Code for form from where it should take data:
HTML
<div class="left">


               Full name: <br>
                   <input name="name" id="name_input" type="text" onclick="myFunction0()" ><br>
           <p id="myP" style="font-size:12px; display:none; color:gray">*Help text here.</p>
               Email: </br>
                   <input id="email" type="email" onclick="myFunction1()"><br>
                   <p id="myP1" style="font-size:12px; display:none; color:gray">*Help text here.</p>
               City <br>
                   <input name="name" id="city" type="text" onclick="myFunction2()" ><br><br>
               <p id="myP2" style="font-size:12px; display:none; color:gray">*Help text here.</p><br><br><br><br><br>

HTML
<div class = "right " style="margin-top:28px;">
	
  Ride in group? <br>
                    <label class="container">Always</label>
                    <input type="radio" id="a" name="feature" value="true">
                    <label class="container">Sometimes</label>
                    <input type="radio" id="s" name="feature" value="false" />
					<label class="container">Never</label>
                    <input type="radio" id="n" name="feature" value="false" /><br><br><br>
					
                
          
			
               
                Days of the week <br>
                    <label >Sun</label>
                    <input type="checkbox" id="sun" name="feature" value="true">
                    <label >Mon</label>
                    <input type="checkbox" id="mon" name="feature" value="false" />
					<label >Tue</label>
                    <input type="checkbox" id="tue" name="feature" value="false" />
					<label >Wen</label>
                    <input type="checkbox" id="wen" name="feature" value="false" />
					<label >Thu</label>
                    <input type="checkbox" id="thu" name="feature" value="false" />
					<label >Fri</label>
                    <input type="checkbox" id="fri" name="feature" value="false" />
					<label >Sat</label>
                    <input type="checkbox" id="sat" name="feature" value="false" /><br><br>
					
					<button type="submit" name="submit" class=" w3-button w3-green" id="add_btn" value="Save" style="margin-right:240px ; float:right " ><a style="font-size:14px; color:white!important ">Save </a></button>
					<button  class=" w3-button w3-gray" id="cancel" value="Cancel" style=" float:right">Cancel </button>
					
                
</div></div>


What I have tried:

I have tried with this code adding new row:
JavaScript
<script>
function addRow() {
  var table = document.getElementById('myTable');
  var columnLength = table.getElementsByTagName('tr')[0].children.length;
  var units = document.getElementsByClassName('unit-table');
  var tr = document.createElement('tr');
  tr.className = 'unit-table';
  for (var i = 0; i < columnLength; i++) {
    var td = document.createElement('td');
    var text = document.createElement('input');
    text.type = 'text';
    td.appendChild(text);
    tr.appendChild(td);
  }
  table.appendChild(tr);
}

document.getElementById('add_btn').onclick = addRow;
</script>
Posted
Updated 23-Feb-18 3:33am
Comments
ZurdoDev 18-Feb-18 21:42pm    
And what is the problem? The JavaScript code you have looks like it adds a row to a table.

Hi,

The problem is in your save button.

After adding rows into the table page gets reloaded ,due to this you are not able to see the changes in the table.
use below code and replace your button control with below:



SCRIPT:

<script type="text/javascript">
        function addRow() {
            var table = document.getElementById('myTable');
            var columnLength = table.getElementsByTagName('tr')[0].children.length;          
            var units = document.getElementsByClassName('unit-table');          
            var tr = document.createElement('tr');
            tr.className = 'unit-table';
            for (var i = 0; i < columnLength; i++) {              
                var td = document.createElement('td');
                var text = document.createElement('input');
                text.type = 'text';
                text.id = 'text' + i;
                if (i == 0) {
                    text.value = document.getElementById('name_input').value;
                } else if (i == 1) {
                    text.value = document.getElementById('email').value;
                } else if (i == 2) {
                text.value = document.getElementById('city').value;
                }
                td.appendChild(text);
                tr.appendChild(td);
            }
            table.appendChild(tr);
        }

        //    document.getElementById('add_btn').onclick = addRow();
    </script>


html part:

<table id="myTable" border="1" cellpadding="5" cellspacing="5"><tbody><tr class="unit-table"><td>                    Full name:
               </td><td>                    Email:
               </td><td>                    City:
               </td></tr></tbody></table>



Save button code:

<input id="Button1" type="button" value="button" onclick="addRow();"/>
 
Share this answer
 
v4
I used to use forms - occasionally I still do - but I've found it's much more convenient to use AJAX. You send your data to the server, processes it, and then returned the processed work back to the screen (in this case, your table). You don't need name=; just an id= and the DOM to retrieve the values you need.

I particularly found it annoying that submitting a form loaded a (new?) page.

I your case, you have <tbody> - so you give it an id and then rebuild your table in your AJAX (with the new data) and return it to replace the old.

A common place where this type of treatment works very well is a table with Yes/No buttons on each row. When you click Yes or No, the data is sent to the server, the answer recorded, and the table refreshed with the new population with the row omitted (or modified). Looks quite nice as the list decreases as it's tended to.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900