Click to See Complete Forum and Search --> : On form, create additional field
fezzik
06-19-2006, 05:50 PM
I'm looking for a ASP solution to create additional fields on a form when the user clicks on either a link or button. I have a javascript example at the following URL:
http://www.tylerlague.com/form/default.htm
If you click on the "+ add another" link under "Affiliations" the javascript creates another field. This allows the user to input multiple affiliations if any.
Is there a ASP solution to create this functionality? Any help is appreciated.
vanny
06-19-2006, 08:17 PM
An ASP solution would rely on on submitting the form to the server, and re dynamically recreating the form with the extra field.
russell
06-19-2006, 09:19 PM
i'm just going to expand a little on what vanny said...
let's say your form element is called "lineno_01" so you have an input like this:
<input name=lineno_1>
You can find this on the server-side like this
Dim maxNum
Dim f
Dim n
maxNum = 0
For Each f in Request.Form
'' first check to see if the element is called "lin??????"
If Left(f, 3) = "lin" Then
n = clng(Right(f, 2))
'' if it is "lin???????" then we want the rightmost characters
If n > maxNum Then maxNum = n
End If
Next
then when you rewrite the form you increment maxNum by one, so you create a loop
Dim i
For i = 0 to maxNum + 1
Response.Write "<input name=lineno_" & i & ">" & vbCrLf
Next
So that's basically how you do it. That said, it is a lot more user friendly if you can accomplish it in JavaScript -- assuming your application is meant to support JavaScript and not support non-JS user agents.
hope this helps. I'm sure lmf232s (http://www.webdeveloper.com/forum/member.php?find=lastposter&t=110699) has a thought on this :cool:
lmf232s
06-20-2006, 09:40 AM
Well since you asked.
Im going to go along w/ what russell has mentioned. When i first started doing this type of thing i did it using asp. I have since moved to javascript to do this type of work but i still use asp for certian situations.
All i do is keep a variable that gets incremented by 1 each time you want to add a field. I keep this value in a hidden field. Then i loop it just like russel did in his example to create the elements, only i get the values from the form a little different. In the end its the same idea and the same results but a slightly different approach.
Also this example is based off the assumption that you name the fields the same but append the value of the counter variable to the end. Also im going to use my select case on this which is IMO a great technique to coding asp pages as it just feels more like VB or .net in the way you can handle certian button clicks which allows you to execute certian sections of code very easily. Also notice to repopulate the text fields i do a request.form("FieldName") for the value of the element. I do this in case it was a database submit but for some reason there was a validation error. The user does not lose any of the data that the entered.
Here is a working example that i just typed up
<%
Option Explicit
Dim i
Dim sValue
Dim intCounter : intCounter = Trim(Request.Form("intCounter"))
Dim hidsubmit : hidsubmit = Trim(Request.Form("hidsubmit"))
Select Case hidsubmit
Case "GetFormValues"
For i = 1 to intCounter
sValue = Trim(Request.Form("lineno_" & i))
Response.Write "The value of lineno_" & i & " is : " & sValue & "<BR>"
Next
Case "AddRow"
intCounter = intCounter + 1
Case "RemoveRow"
If intCounter > 0 Then
intCounter = intCounter - 1
Else
Response.Write "There are no more rows to remove"
End If
Case Else
intCounter = 1
End Select
%>
<body>
<form name=form1 method=Post>
<input type=hidden name=hidsubmit value="">
<input type=hidden name=intCounter value="<%=intCounter%>">
<%
Response.write "<table>"
For i = 1 To intCounter
Response.write "<tr>"
Response.Write "<td><input name=""lineno_" & i & """ value=""" & Request.Form("lineno_" & i) & """></td>"
Response.write "</tr>"
Next
Response.Write "<tr>"
Response.Write "<td>"
Response.Write "<input type=button name=cmdAddRow value=""Add Row"" onclick=""document.form1.hidsubmit.value='AddRow';document.form1.submit();""><br>"
Response.Write "<input type=button name=cmdRemoveRow value=""Remove Row"" onclick=""document.form1.hidsubmit.value='RemoveRow';document.form1.submit();""><br>"
Response.Write "<input type=button name=cmdSubmit value=""Submit Form Values"" onclick=""document.form1.hidsubmit.value='GetFormValues';document.form1.submit();""><br>"
Response.Write "</td>"
Response.Write "</tr>"
Response.write "</table>"
%>
</form>
</body>
russell
06-20-2006, 10:05 AM
nice