<script language="javascript" type="text/javascript">
function addPerson()
{
if (navigator.userAgent.toLowerCase().indexOf("msie") !=
-1){
var newid =
document.getElementById('personContainer').childNodes.length + 1;
}
else{
var newid =
document.getElementById('personContainer').childNodes.length - 1;
}
tmpdiv = document.createElement('DIV');
tmpdiv.id = "nameset" + newid;
tmpdiv.innerHTML = 'First name: <input type="text"
name="fname' + newid + '" size="30" maxlength="30" /> Last name: <input
type="text" name="lname' + newid + '" size="30" maxlength="30" /> [' + newid
+ ']';
document.getElementById('personContainer').appendChild(tmpdiv);
document.myform.fieldcount.value=newid;
}
</script>
<form name="myform" action="" method="post">
<input type="hidden" name="fieldcount" value="1" /><br />
<a href="javascript:void(0)" onclick="addPerson()">[ Add Person
]</a><br />
<div id="personContainer">
<div id="nameset1">First name: <input type="text" name="fname1"
size="30" maxlength="30" /> Last name: <input type="text" name="lname1"
size="30" maxlength="30" /> [1]</div>
</div>
</form>
Try that out for the user end. All the created fields will exist in the form
scope on the action page. Use the fieldcount field to decide how many fname
and lname fields you have.
To loop over the fname/lname pairs try this.
<cfset loopto = form.fieldcount / 2 />
<cfloop from="1" to="#loopto#" index="I">
#form["fname" & i]# #form["lname" & i]#<br />
</cfloop>
Let me know if you have any issues with it. I only looked at it in IE 6 and
the latest FF (VERSION 2 TOMORROW PEOPLE!)
Between IE6 and FF, the childNodes count was off and that's why there's a
small browser detection in there.
-----Original Message-----
From: Daniel Kessler [mailto:[EMAIL PROTECTED]
Sent: Monday, October 23, 2006 1:20 PM
To: CF-Talk
Subject: dynamically add to a form
I have an application that keeps notes on a meeting. I have fields
for the names of attendees (lname, fname). I want to be able to add
as many attendees as necessary. I was going to put a fname_1 and
lname_1 field in it and a "add a person" link. The link would submit
the page with an action=add_attendee, then I would do structKeyExists
to loop through how many people are already there to fill up the
previous entries and add an entry.
I've never done this before, does this sound like a good plan?
--
Daniel Kessler
College of Health and Human Performance
University of Maryland
Suite 2387 Valley Drive
College Park, MD 20742-2611
Phone: 301-405-2545
http://hhp.umd.edu
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Introducing the Fusion Authority Quarterly Update. 80 pages of hard-hitting,
up-to-date ColdFusion information by your peers, delivered to your door four
times a year.
http://www.fusionauthority.com/quarterly
Archive:
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:257779
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe:
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4