PDA

View Full Version : Adding Dynamic Form Fields to Access Database using ASP


SUNNY99
05-11-2009, 07:23 PM
Adding Dynamic Form Fields to Access Database using ASP

I am trying to create a form with dynamic form Fields.

I have set up javascript to dynamically display number of Text fields based on drop down selection.

When the submit button is hit it should run asp script file to add the field data into a MS access database.

I have uploaded the html file at following site.

www12.asphost4free.com/sunny99/Test.html

I will appreciate if anyone can help me doing this with ASP

Say user chooses 3 names to add. (3 text boxes display)

How do I dynamically create these 3 fields in access database using ASP?

How do i then insert these values into these 3 new fields?

Thanks for ur help

HTML CODE for the file is listed below

***********************


<html>
<head>
<title>untitled</title>
<style type="text/css">

div.inputbox {
width: 200px;
font-size: 80%;
padding: 3px 0px 3px 50px;
}

</style>


<script type="text/javascript" language="javascript">

function setTextInputs(oSelect) {
var howmany = oSelect[oSelect.selectedIndex].value, add = true;
var n = 1, oForm = oSelect.form, oDiv, oInput;
while (oDiv = document.getElementById('Name' + n))
if (n++ > howmany) {
add = false;
oForm.removeChild(oDiv);
}
if (add)
for (n; n <= howmany; ++n) {
oDiv = document.createElement('div');
oDiv.setAttribute('id', 'Name' + n);
oDiv.className = 'inputbox';
oDiv.appendChild(document.createTextNode('Name ' + n + ': '));
oForm.appendChild(oDiv);

oInput = document.createElement('input');
oInput.setAttribute('type', 'text');
oInput.setAttribute('name', 'Name' + n);
oDiv.appendChild(oInput);
}
}

</script>
</head>
<body>
<form>
<p><strong>Number of People: </strong>
<select name="NoOfPpl" onChange="setTextInputs(this)">
<option value="0" selected="selected">- 0 -</option>
<option value="1">- 1 -</option>
<option value="2">- 2 -</option>
<option value="3">- 3 -</option>
<option value="4">- 4 -</option>
<option value="5">- 5 -</option>
<option value="6">- 6 -</option>
<option value="7">- 7 -</option>
<option value="10">- 10 -</option>
</select></p>
</form>
<label>
<input type="submit" name="Submit" value="Submit">
</label>
</body>
</html>

***********************

Old Pedant
05-11-2009, 09:20 PM
You are approaching this all wrong.

You do *NOT* want to modify the database tables *AT ALL* if you EVERY can help it!

Instead, you need to use a "one to many" table and then you simply put each name into a separate *record* in that table.

Example: Let's say you allow people to register themselves and then they can recommend up to 10 people to be added to their "friends" list:

TABLE: Members
memberid : int, primary key, probably AUTONUMBER
membername : text
memberemail : text
memberpassword : text

TABLE: Friends
memberid : int, foreign key to Members table
friendname : text
friendemail : text


So now your <form> may have zero to 10 fields for friend names (and 0 to 10 for their email addresses), but your database table is UNCHANGED.

As you handle each friend in the posted to code, you just add another record to the Friends table.

************

If you show more of your REAL application and your real database table, we can show you ways to adapt this to your code. But do *NOT* alter the database table.