Link to home
Start Free TrialLog in
Avatar of gifford_c
gifford_c

asked on

Adding a row to a table and submit data

I'm new to JavaScript and I'm at the 1st stages of learning.  I've got the script below that does almost everything I want but, I want the textboxes in a line rather than on top of each other?  The submitting part works fine, its just if I move the textboxes next to each other I get error.

<html>
<head>
<script language="javascript">
function addRow(tbl){
 var tb = tbl.firstChild;
 var ind = tb.childNodes.length;
 var nr = document.getElementById('cloneThis').cloneNode(true);
 nr.style.display = "block";
 with(nr.firstChild.firstChild.firstChild.childNodes){
   item(0).childNodes[1].innerHTML = "<input type=text name=name" + ind + " />";
   item(1).childNodes[1].innerHTML = "<input type=text name=phone" + ind + " />"
 }
 tb.appendChild(nr);
 if (tbl.rows.length > 1) {
   document.getElementById('delRow').disabled = false;
 }
 document.forms[0].count.value = ind + 1;
}

function deletRow(tbl){
  if (tbl.rows.length > 1) {
    tblRow = tbl.deleteRow(tbl.rows.length-1);
    //           alert(tbl.rows.length);
  }
}
function disablit(tbl){
  if(tbl.rows.length == 1) {
    document.getElementById('delRow').disabled = true;
  }
}
</script>
</head>
<body>
<%
  count = request.form("count")
  for i = 0 to cint(count) - 1
    name = request.form("name" & i)
    phone = request.form("phone" & i)
    response.write "name = " & name & "<br>"
    response.write "phone = " & phone & "<br>"
  next
   
%>

<form name="addAccount" method="post" >
<input type=hidden name="count" value="1">
<table id="purchase">
<tr id='cloneThis'>
<td>
<table>
<tr>
<td>Name</td>
<td colspan=5><input type="text" name="name0"></td>
</tr>
<tr>
<td>Phone</td>
<td colspan=5><input type="text" name="phone0"</td>
</tr>
</table>
</td></tr>
</table>
<input type=button onclick="addRow(document.getElementById('purchase'))" value="Add">
<input type=button id="delRow" name="delRow" onclick="deletRow(document.getElementById('purchase'));disablit(addContact);" value="Delete" disabled>
<br>
<br>
<input type="submit" value="submit">
</form>
</body>
</html>
Avatar of william_jwd
william_jwd
Flag of United States of America image

I dont find any textbox in the code you have posted ???
Is this what you are looking for,

<html>
<head>
<script language="javascript">
function addRow(tbl){
 var tb = tbl.firstChild;
 var ind = tb.childNodes.length;
 var nr = document.getElementById('cloneThis').cloneNode(true);
 nr.style.display = "block";
 with(nr.firstChild.firstChild.firstChild.childNodes){
   item(0).childNodes[1].innerHTML = "<input type=text name=name" + ind + " />";
   item(0).childNodes[1].innerHTML = "<input type=text name=phone" + ind + " />"
 }
 tb.appendChild(nr);
 if (tbl.rows.length > 1) {
   document.getElementById('delRow').disabled = false;
 }
 document.forms[0].count.value = ind + 1;
}

function deletRow(tbl){
  if (tbl.rows.length > 1) {
    tblRow = tbl.deleteRow(tbl.rows.length-1);
    //           alert(tbl.rows.length);
  }
}
function disablit(tbl){
  if(tbl.rows.length == 1) {
    document.getElementById('delRow').disabled = true;
  }
}
</script>
</head>
<body>
<%
  count = request.form("count")
  for i = 0 to cint(count) - 1
    name = request.form("name" & i)
    phone = request.form("phone" & i)
    response.write "name = " & name & "<br>"
    response.write "phone = " & phone & "<br>"
  next
   
%>

<form name="addAccount" method="post" >
<input type=hidden name="count" value="1">
<table id="purchase">
<tr id='cloneThis'>
<td>
<table>
<tr>
<td>Name</td>
<td colspan=5><input type="text" name="name0"></td>
<td>Phone</td>
<td colspan=5><input type="text" name="phone0"</td>
</tr>
</table>
</td></tr>
</table>
<input type=button onclick="addRow(document.getElementById('purchase'))" value="Add">
<input type=button id="delRow" name="delRow" onclick="deletRow(document.getElementById('purchase'));disablit(addContact);" value="Delete" disabled>
<br>
<br>
<input type="submit" value="submit">
</form>
</body>
</html>
Avatar of gifford_c
gifford_c

ASKER

The text boxes are side by side by when you click on Add the phone entry isn't blank it copies what you put in the top box, also when you submit they are seperated by comma, which I dont want.
Avatar of Zvonko
Just the same code I made yesterday :-)
http:Q_20929157.html

<html>
<head>
    <title>Untitled</title>
    <script language="javascript">
    function addRow(){
      var tr = document.getElementById('cloneThis');
      var tb = tr.parentNode;
      var ind = tb.rows.length;
      var nr = tr.cloneNode(true);
      nr.id = "R"+ind;      
      tb.appendChild(nr);
      theForm = document.forms[0];
      newFirstName = theForm.contactFName;
      newLastName = theForm.contactLName;
      newPhone = theForm.contactPhone;
      newFirstName = newFirstName[newFirstName.length-1];
      newFirstName.value = "";  
      newFirstName.name = "fname" + ind;  
      newFirstName.id = "fname" + ind;  
      newLastName = newLastName[newLastName.length-1];
      newLastName.value = "";  
      newLastName.name = "lname" + ind;  
      newLastName.id = "lname" + ind;  
      newPhone = newPhone[newPhone.length-1];
      newPhone.value = "";  
      newPhone.name = "phone" + ind;  
      newPhone.id = "phone" + ind;  
      if (tb.rows.length > 2) {
        var elem = document.getElementById('delRow');
        elem.disabled = false;
        elem.style.display = "block";
       }
    }


    function deletRow(tblName){
      var tbl = document.getElementById(tblName);
      if (tbl.rows.length > 2) {
        tblRow = tbl.deleteRow(tbl.rows.length-1);
        if(tbl.rows.length < 3) {
           var elem = document.getElementById('delRow')
           elem.disabled = true;
           elem.style.display = "none";
        }
      }
    }


</script>
</head>
<body>
<form>
<DIV>
<table cellspacing="0" cellpadding="3" border="1" width="100%">
    <tr>
         <td>
              <table cellspacing="0" cellpadding="0" border="0" width="100%" id="addContact">
                   <tr>
                        <td colspan="2">Account Contact(s) Information</td>
                   </tr>
                   <tr id='cloneThis'>
                        <td colspan="2">
                              <table cellspacing="0" cellpadding="3" border="1" width="100%">
                                  <tr>
                                       <td>*Name</td>
                                       <td colspan="5"><input type="text" name="contactFName" value="<%=contactFName%>">&nbsp;&nbsp;LName&nbsp;<input type="text" name="contactLName" value="<%=contactLName%>"></td>
                                  </tr>
                                  <tr>
                                       <td>*Phone</td>
                                       <td colspan="5"><input type="text" name="contactPhone" value="<%=contactPhone%>"></td>
                                  </tr>
                                  <tr><td colspan="6">&nbsp;</td></tr>
                             </table>
                        </td>
                   </tr>
              </table>
         </td>
    </tr>
    <tr>
         <td>
              <div style="float:left;"><input type=button onclick="addRow('addContact')" value="Add another contact?"></div>
             <input type=button id="delRow" name="delRow" onclick="deletRow('addContact');" value="Cancel" disabled style="display:none">
         </td>
    </tr>    
</table>
</form>
</body>
</html>

ASKER CERTIFIED SOLUTION
Avatar of william_jwd
william_jwd
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial