A trickier HTML Form create new table(s) | Member | | Join Date: Sep 2008
Posts: 36
| | |
Hey guys, another part of program I am stuck at is to create an email storage / reference system. I need the first line to hold the basic info, from to address etc and then a second row to store the message and any comments in a text box. The problem I am having is that I put the two into different tables so when a new line is added it adds to both them. Is it possible to do this e.g to have on the second add to append it to the bottom table?
I To | | From | | Date | | Subject |
| Message | | onClick() |
Which would go to
I To | | From | | Date | | Subject |
| Message |
I To | | From | | Date | | Subject |
| Message | | onClick() |
And so on. Here is the code i have so far ....
[HTML]<html>
<head>
<script type="text/javascript">
function removeRowFromTable()
{
var tbl = document.getElementById('myTable');
var lastRow = tbl.rows.length;
if (lastRow > 2) tbl.deleteRow(lastRow - 1);
var tbl1 = document.getElementById('myTable1');
var lastRow1 = tbl1.rows.length;
if (lastRow1 > 2) tbl1.deleteRow(lastRow1 - 1);
}
</script>
<script type="text/javascript">
function doThis()
{
var myTab = document.getElementById('myTable');
var row=myTab.rows.length;
var y=myTab.insertRow(row);
var a=y.insertCell(0);
var xx= document.createElement('input');
xx.type="text";
xx.name="EmailFrom[]";
a.appendChild(xx);
var b=y.insertCell(1);
var xx= document.createElement('input');
xx.type="text";
xx.name="EmailTo[]";
b.appendChild(xx);
var c=y.insertCell(2);
var xx= document.createElement('input');
xx.type="text";
xx.name="EmailDate[]";
c.appendChild(xx);
var d=y.insertCell(3);
var xx= document.createElement('input');
xx.type="text";
xx.name="EmailSubject[]";
d.appendChild(xx);
var myTab1 = document.getElementById('myTable1');
var row1=myTab1.rows.length;
var z=myTab1.insertRow(row1);
var e=z.insertCell(0);
var xx= document.createElement('textarea');
xx.col="200"
xx.rows="5"
xx.name="Message[]";
e.appendChild(xx);
}
</script>
</head>
<body>
<br/>
<input type="button" value="Add" onclick="doThis()">
<input type="button" value="Remove" onClick="removeRowFromTable()">
<br/>
<table id="myTable" border="1" cellspacing="5" cellpadding="5">
<tr>
<th>From</th>
<th>To</th>
<th>Date</th>
<th>Subject</th>
</tr>
<table id="myTable1" border="1" cellspacing="5" cellpadding="5">
<tr>
<th>Message</th>
</tr>
</table>
</table>
</body>
</html>[/HTML]
Any help much appreciated
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: A trickier HTML Form create new table(s)
The second table has been (incorrectly) nested in the first.
Please use code tags when posting code. See How to ask a question.
| | Member | | Join Date: Sep 2008
Posts: 36
| | | re: A trickier HTML Form create new table(s)
my apologies im fairly new to this forum but in future i will post with regards to the "how to..."
In relation to what you said do you mean that you cannot nest tables like this or that I have nested it wrongly but it can work.
what are your thoughts on this?
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: A trickier HTML Form create new table(s)
I haven't tested your code, but I noticed that myTable1 is nested inside myTable after a row, but not inside a table cell. Did you mean to nest tables like that or was it supposed to be outside the main table?
| | Member | | Join Date: Sep 2008
Posts: 36
| | | re: A trickier HTML Form create new table(s)
No it is meant to be inside the table but im not 100% sure if it is the way to go... the real question im asking is can i have one table that will have two different rows of headers. e.g. can a table be
| 1 | | 2 | | 3 |
|input 1| | input2| |input3|
| 4 |
| input 4 |
then onclick will add another full two rows for input.
the reason y im looking this is ive been asked to store emails so correspondence can be viewed (along with alot of other info) with in the one system. The from, to and subject are all low character fields whereas the email can be quite large and so doing it
| 1 | | 2 | |3 | |4 | will not look good as input 4 is a text area around 60 by 5.
hope i have been able to clarify the problem a bit more :)
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: A trickier HTML Form create new table(s)
How would you match up the rows? Say you have 3 rows. If the first three columns are separated from the message column, how will the users know which refers to which?
PS. to answer your question on whether it's possible, it is.
| | Member | | Join Date: Sep 2008
Posts: 36
| | | re: A trickier HTML Form create new table(s)
that was the problem... from the way the code works now it isnt the prettiest to look at... as a first step i have just attached it to another column at the end of the first three inputs but again it doesnt really look right. the only other way i can think of doing it is that on the first pass it will do as normal but then passes after this will add to the second table although headings will not be there. Have you any ideas on a better solution cus i am more r less a noob with html.
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: A trickier HTML Form create new table(s)
One way it could work is that you display the message textarea without a header underneath the other three columns on its own.
| | Member | | Join Date: Sep 2008
Posts: 36
| | | re: A trickier HTML Form create new table(s)
That would still leave the problem that each new record would be stored under the headings and the message field would store underneath it...
| 1 | | 2 | 3 |
inputs
| :::::::::::::::::::::::::::::|
2nd pass
| 1 | | 2 | | 3 |
inputs
inputs
|::::::::::::::::::::::::::::::|
|::::::::::::::::::::::::::::::|
As I am appending to the table, is this was you mean?
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: A trickier HTML Form create new table(s)
No, I meant something like this: - | 1 | | 2 | 3 |
-
inputs
-
| :::::::::::::::::::::::::::::|
-
2nd pass - | 1 | | 2 | | 3 |
-
inputs
-
|::::::::::::::::::::::::::::::|
-
inputs
-
|::::::::::::::::::::::::::::::|
-
Is that OK?
| | Member | | Join Date: Sep 2008
Posts: 36
| | | re: A trickier HTML Form create new table(s)
yeh that would be perfect... how would u code it compared to what i have?
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: A trickier HTML Form create new table(s)
Instead of adding/nesting another table, just add another row to myTable with insertRow(). Set the colSpan to 3 to cover all columns. When deleting, delete two rows.
| | Member | | Join Date: Sep 2008
Posts: 36
| | | re: A trickier HTML Form create new table(s)
sound one ill try that now. Thanks for the help.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,419 network members.
|