Avatar of diecasthft01
diecasthft01
 asked on

Retrieving from data from added rows in javascript

Good morning, I have a question related to forms and selects when using javascript. I have a requirement to allow the user to click a button on a web page to add additional rows of information on a web page. every time they click a button, it adds a new row with four text boxes and one drop down box to the web page for data entry. Works fine with text boxes, but Ive added a drop down box to the rows and cant figure out how to grab that drop down select for inserting into a database. I need to grab data from any rows added to insert into a table in SQL Server


My script is:

<script language="javascript">
    row_no=1;
    function addRow(tbl,row){
        //so that user can only add 8 rows
        if(row_no<=7){    
        var textbox='<input type="text" name="textfield[]">';//for text box
        var remove= '<a href="#" onclick="removeRow(\''+ tbl +'\',\'' + row_no + '\')"/></a><br>';
   
      //for the text which is used to remove the current row by using the function removeRow(..,..)
       
        //for suitable label to the row
        if(row_no==1) {
      textbox='<input type="text" name="txtfield1" size="10">&nbsp;&nbsp;<input type="text" name="txtfield1a" size="10">&nbsp;&nbsp;<input type="text" name="txtfield1b" size="10" class="style2color13addblock"><input type="text" name="txtfield1C" size="10" class="style2color13addblock">';
        text="<div class='label' align=left></div>";
      }
      else if(row_no==2) {
      textbox='<input type="text" name="txtfield2" size="10">&nbsp;&nbsp;<input type="text" name="txtfield2a" size="50">&nbsp;&nbsp;<input type="text" name="txtfield2b" size="10" class="style2color13addblock">';
      text="<div class='label' align=right></div>";
       }
        else if(row_no==3) {
      textbox='<input type="text" name="txtfield3" size="10">&nbsp;&nbsp;<input type="text" name="txtfield3a" size="50">&nbsp;&nbsp;<input type="text" name="txtfield3b" size="10" class="style2color13addblock">';
      text="<div class='label' align=right></div>";
      }
     
     
      else if(row_no==4) {
      textbox='<input type="text" name="txtfield4" size="10">&nbsp;&nbsp;<input type="text" name="txtfield4a" size="50">&nbsp;&nbsp;<input type="text" name="txtfield4b" size="10" class="style2color13addblock">';
      text="<div class='label' align=right></div>";
      }  
         
      else if(row_no==5) {
      textbox='<input type="text" name="txtfield5" size="10">&nbsp;&nbsp;<input type="text" name="txtfield5a" size="50">&nbsp;&nbsp;<input type="text" name="txtfield5b" size="10" class="style2color13addblock">';
      text="<div class='label' align=right></div>";
      }
         
      else if(row_no==6) {
      textbox='<input type="text" name="txtfield6" size="10">&nbsp;&nbsp;<input type="text" name="txtfield6a" size="50">&nbsp;&nbsp;<input type="text" name="txtfield6b" size="10" class="style2color13addblock">';
      text="<div class='label' align=right></div>";
      }
      else if(row_no==7) {
      textbox='<input type="text" name="txtfield7" size="10">&nbsp;&nbsp;<input type="text" name="txtfield7a" size="50">&nbsp;&nbsp;<input type="text" name="txtfield7b" size="10" class="style2color13addblock">';
      text="<div class='label' align=right></div>";
      }
var tbl = document.getElementById(tbl);//to identify the table in which the row will get insert
        var rowIndex = document.getElementById(row).value;//to identify the row after which the row will be inserted
        try {
            var newRow = tbl.insertRow(row_no);//creation of new row
            var newCell1 = newRow.insertCell(0);//first  cell in the row
            newCell1.innerHTML = text;//insertion of the 'text' variable in first cell
         
         var newCell2 = newRow.insertCell(1);//first  cell in the row
            newCell2.innerHTML = text;//insertion of the 'text' variable in first cell
         
         var newCell3 = newRow.insertCell(2);//first  cell in the row
            newCell3.innerHTML = text;//insertion of the 'text' variable in first cell
         
    var cell4 = newRow.insertCell(3);
    var element4 = document.createElement("select");
    var option1 = document.createElement("option");
    option1.innerHTML = "Option1";
    option1.value = "1";
    element4.add(option1, null);
    var option2 = document.createElement("option");
    option2.innerHTML = "Option2";
    option2.value = "2";
    element4.add(option2, null);
    cell4.appendChild(element4);
         
    var newCell = newRow.insertCell(1);//second cell in the row
            newCell.innerHTML = textbox + " " + remove;//insertion of the text box and the remove text using their variable
            row_no++;
        } catch (ex) {
            alert(ex); //if exception occurs
        }  
    }
    if(row_no>7)//if the row contain 3 textbox, the add button will disapper
    {
        document.getElementById("add").style.display="none";
    }                        
}
function removeRow(tbl,num)
{
    var table = document.getElementById(tbl);//adentification of table
    try {
        row_no--;
        table.deleteRow(num);//deletion of the clicked row
    } catch (ex) {
        alert(ex);
    }
    if(row_no<=7)//if row is less than 8 then the button will again appear to add row
    {
            document.getElementById("add").style.display="block";
    }    
} </script>


And my HTML is:

<form id="Form_RequestAccess" name="Form_RequestAccess" method="post" action="InsertForm.cfm">


<table width="900" cellspacing="2" cellpadding="1" id="mytable">

   
    <tr id="myrow"><td></td><td></td><td></td><td></td><td></td>
    </tr>
   
    <tr ID="add" height="20">
      <td width="6"></td><td align="left"><input type="button" name="Button" value="Add IT Item" onClick="addRow('mytable','myrow')"></td>
    </tr>
  </table>  

<table width="900">
   <col style="width:22px">  
   <tr>
   <td></td>
   <td><input name="submit" type="submit" value="Save Draft">
      <input name="reset" type="reset" style="margin-right: 166" value="Discard Changes"></td></tr></table>  
</form>

JavaScript

Avatar of undefined
Last Comment
diecasthft01

8/22/2022 - Mon
Michel Plungjan

Currently the script does nothing

Can you please make a jsfiddle.net version that actually does something

I started this one https://jsfiddle.net/mplungjan/e94b3wn6/ where I
* remove the name="submit"
* use addEventListener
* added tbody and thead

What is the deal with row < 7 and why not just add and change the name or if you are using PHP just name all fields with a [] after - like txtfield[] and txtfieldb[] and have the PHP handle the array they send?

the whole script can be just a few lines of code - for example using cloneNode
diecasthft01

ASKER
For whatever reason, it didnt work in jsfiddle....works fine in my development environment. I'll figure something out to get what I want.
Michel Plungjan

Here is an updated example


Max 8 rows in 10 lines of code

https://jsfiddle.net/mplungjan/e94b3wn6/
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
diecasthft01

ASKER
That didn't seem to work....wouldnt add any lines. I did scale all of the other stuff down and did a new page.....same result that I want...rows added with text firlds and a drop down. I'm just not sure how to get the info into the database. Normally Ive always done static fields where the inserts are easy based on a form name...I dont have that with dynamically added fields/rows.
https://jsfiddle.net/pwyLedjf/#&togetherjs=EaCf2jGaMO

Michel Plungjan

You need to NOT have the script tags in the javascript panel

Also the type cannot be text1 and tetxt2, perhaps you meant the name and they should be

 element1.type = "text";
 element1.name = "text1[]";

for example


https://jsfiddle.net/mplungjan/a2pnqxLj/
Michel Plungjan

I corrected some HTML and simplified the script
You cannot call the submit button name="submit" if you ever want to control the submission using script
It hides the form.submit method
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
diecasthft01

ASKER
Oops...yes, I meant name......

I renamed the submit button to "submit1"

I may be confused with something though. You say I dont need the <script> and </script> tags?
So this is working with adding the rows and the dropdown, but Im still confused as to how its sending the form data to an action page. Im using CF, and Im guessing the [] at the end of each name is some sort of row num? But Im not sure what the action page would be looking for. I did a cfoutput just to see if I was capturing the form and got an error. 
Michel Plungjan

You cannot have <SCRIPT> tags in the JSFIDDLE JavaScript Pane, like you cannot have <style> tags in the CSS pane
Michel Plungjan

Ah, sorry, You had not mentioned CF. then no point in the [], I'll change the script for you

https://jsfiddle.net/mplungjan/a2pnqxLj/

Using the rowcount now
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
Michel Plungjan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
diecasthft01

ASKER
Im getting an error somewhere long the line...Im back to not being able to get the rows to come up 
lenamtl

Hi,
I would not recommend to place a form into a table.
Nowadays we use div, using Bootstrap you can set up the civ column to achieve the same visual and this will be a lot more easier to manage.
https://getbootstrap.com/
diecasthft01

ASKER
I was able to take some of what I had, the many good ideas that you gave me and I believe I have what I need...thanks a lot!!!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.