Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Someone help me!!! Please! :O)

Status
Not open for further replies.

Sheltered

Programmer
Nov 26, 2002
55
GB
Hi people, hopefully someone can help me out here.
Its quite complicated to explain but hopefully its clear enough.

I have a web page which consists of a form, which is simply two text input fields.

When the form is completed and the 'Submit (Add Items)' button pressed the contents of the form are displayed in a table within the same page (just below the form).

A feature i need on this page is that you can add/display more than one row of form data at a time.
I actually found some javascript right here that allows me to click a button and a new row is created in the form.

So it looks to be working great, you complete the first row, click the 'Add Row' button and another row appears in the form.

However... i have two problems, im new to javascript so it could be a simple problem.
when i click the 'Add Item(s)' button, only one row is displayed on the page. If there are several rows in the form it will only display the last row in the new table.
I suspect that this is simply down to my bad coding.
The biggest problem i have is that i want to be able to save the data submitted by the form to a table in the web page, i.e. i add two rows of data then exit my browser. i want this data to be displayed when i next open this page, and i want to be able to add to it aswell.

Having searched and read many topics here and tutorials i now know that the saving bit cant be done with javascript.

I also know it can be done with ASP and a databse, however, i dont have the resources or knowledge to do it this way. Anyone know another way this can be saved and updated? Or can anyone provide the ASP coding to write it to a databse as i've never got round to learning ASP as yet.

I'll paste in the webpage i have so far so you can see where im at.

Any help with either these problems would be great.
Thanks
Pete


Ok, heres the page so far, what am i doing wrong??

<html>
<head>
<title> Insert and Save </title>
<script language=&quot;JavaScript&quot;>
function addRow() {
var oRow;
var oCell1;
var oCell2;
var newnumber;


newnumber = dynatable.rows.length;
oRow = dynatable.insertRow();
oCell1 = oRow.insertCell();
oCell1.innerHTML = &quot;<input type='text' id='item&quot; + newnumber + &quot;'>&quot;;
oCell2 = oRow.insertCell();
oCell2.innerHTML = &quot;<input type='text' id='notes&quot; + newnumber + &quot;' size='75'>&quot;;
form1.numberRows.value++;
}

function test() {
var rows;
rows = form1.numberRows.value
//alert(rows)
for (i=1; i<=rows; i++) {
var string='form1.item'+i+'.value';
var string2='form1.notes'+i+'.value';
//alert(string + ' ' + string2)
document.all.disp.innerHTML = '<table align=&quot;center&quot; border=&quot;1&quot;><tr><td align=&quot;center&quot;>'+string+'</td><td

align=&quot;center&quot;>'+string2+'</td></tr></table>'
}
}
</script>
</head>
<body>
<form name=&quot;form1&quot; method=&quot;POST&quot;>
<input type=&quot;button&quot; name=&quot;Button&quot; value=&quot;Add Row&quot; onClick=&quot;addRow()&quot;>
<table border=&quot;1&quot; id=&quot;dynatable&quot;>
<thead>
<tr bgcolor=&quot;powderblue&quot;>
<td><strong>Date</strong></td>
<td><strong>Notes</strong></td>
</tr>
</thead>
<tbody id=&quot;TheBody&quot;>
<tr>
<td><input type=&quot;text&quot; id=&quot;item1&quot;></td>
<td><input id=&quot;notes1&quot; type=&quot;text&quot; size=&quot;75&quot;></td>
</tr>
</tbody>
</table>
<input type=&quot;button&quot; name=&quot;Submit&quot; value=&quot;Add Item(s)&quot; onClick=&quot;test()&quot;>
<input type=&quot;hidden&quot; name=&quot;MM_insert&quot; value=&quot;forma&quot;>
<input name=&quot;numberRows&quot; type=&quot;hidden&quot; id=&quot;numberRows&quot; value=&quot;1&quot;>
</form>
<hr />

<span id=&quot;disp&quot; style=&quot;position:relative&quot;> </span>

</body>
</html>
 
Referring to your main proble, it's way over my head but basically yes, you cannot do that with javascript. You definitly need some server side script. Asp will work as you mention but so will php or perl for example.
All you need is a basic script that will write the submitted details to a database or flat file, then read those values and echo them into your table.

Devshed.com have some good articles on how to write/read to/from databases and flat files. A quick search on google for &quot;content management&quot; or other such scripts will give you lots of options.



É
::
 
To remember the values after loading you need to set and read a cookie, I don't have code for that yet. The other problem is that you set the innerHTML within the loop so only the last row is displayed, here is a working code.

<html>
<head>
<title> Insert and Save </title>
<script language=&quot;JavaScript&quot;>
function addRow() {
var oRow;
var oCell1;
var oCell2;
var newnumber;


newnumber = dynatable.rows.length;
oRow = dynatable.insertRow();
oCell1 = oRow.insertCell();
oCell1.innerHTML = &quot;<input type='text' id='item&quot; + newnumber + &quot;'>&quot;;
oCell2 = oRow.insertCell();
oCell2.innerHTML = &quot;<input type='text' id='notes&quot; + newnumber + &quot;' size='75'>&quot;;
form1.numberRows.value++;
}

function test() {
var rows;
var arrContent = new Array();
arrContent[arrContent.length ] = '<table align=&quot;center&quot; border=&quot;1&quot;>';
rows = form1.numberRows.value
//alert(rows)
for (i=1; i<=rows; i++) {
var string= document.getElementById('item'+i).value;
var string2=document.getElementById('notes'+i).value;
//alert(string + ' ' + string2)
arrContent[arrContent.length] = '<tr><td align=&quot;center&quot;>';
arrContent[arrContent.length] = string;
arrContent[arrContent.length] = '</td><td align=&quot;center&quot;>';
arrContent[arrContent.length] = string2;
arrContent[arrContent.length] = '</td></tr>';
}
arrContent[arrContent.length]='</table>';
document.all.disp.innerHTML = arrContent.join(&quot;&quot;);
}
</script>
</head>
<body>
<form name=&quot;form1&quot; method=&quot;POST&quot;>
<input type=&quot;button&quot; name=&quot;Button&quot; value=&quot;Add Row&quot; onClick=&quot;addRow()&quot;>
<table border=&quot;1&quot; id=&quot;dynatable&quot;>
<thead>
<tr bgcolor=&quot;powderblue&quot;>
<td><strong>Date</strong></td>
<td><strong>Notes</strong></td>
</tr>
</thead>
<tbody id=&quot;TheBody&quot;>
<tr>
<td><input type=&quot;text&quot; id=&quot;item1&quot;></td>
<td><input id=&quot;notes1&quot; type=&quot;text&quot; size=&quot;75&quot;></td>
</tr>
</tbody>
</table>
<input type=&quot;button&quot; name=&quot;Submit&quot; value=&quot;Add Item(s)&quot; onClick=&quot;test()&quot;>
<input type=&quot;hidden&quot; name=&quot;MM_insert&quot; value=&quot;forma&quot;>
<input name=&quot;numberRows&quot; type=&quot;hidden&quot; id=&quot;numberRows&quot; value=&quot;1&quot;>
</form>
<hr />

<span id=&quot;disp&quot; style=&quot;position:relative&quot;> </span>

</body>
</html>
 
Sorry, I didn't read your question correctly. You want to save to a database. This works with IE 6:

To insert data in a database you can use ADO on windows, here is an example using access 2000:

string1 = &quot;hello&quot;;
string2 = &quot;bye&quot;;
var conn = new ActiveXObject(&quot;ADODB.Connection&quot;);
conn.Open(&quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\db1.mdb;Persist Security Info=False&quot;);
conn.Execute(&quot;insert into
([field1],[field2]) values ('&quot;+string1+&quot;','&quot;+string2+&quot;')&quot;);

In the strings you should replace ' with ''.
If the first string is a data you should not let the user fill this out but use a popup calendar.

When loading the page you have to open a recordset and fill an array with the data, then display it.

I don't have time to write the code now so you have to try this yourselve.

 
Thanks to you both, much appreciated. I'll have a play around and hopefully i can suss it out.
[wiggle]
cheers again

Pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top