Variable Number of Input Fields in Classic-ASP form

橙三吉。 提交于 2019-12-23 21:36:04

问题


I have a check out form where number of products can be "n". So how i can know how many input fields are in the form and take input from it?

Thanks


回答1:


If it's a group of single controls - say a variable number of checkboxes representing items - the solution is pretty straightforward. For your checkboxes:

<input type="checkbox" name="ProductID" value="1" />Product #1<br />
<input type="checkbox" name="ProductID" value="2" />Product #2<br />
<input type="checkbox" name="ProductID" value="3" />Product #3

Then in your ASP, you could do this:

<%
  Dim intID

  For Each intID In Request.Form("ProductID")
    ' intID now represents a selected product ID.  Insert into DB
    ' or whatever your process is.  Note that only the "checked" values
    ' will be passed to the server.
  Next
%>

In fact, this approach will work for any number of controls with the same name. If it were 1 - n number of textboxes with the name "FavoriteColor", you could For Each through each value in the same manner. Textboxes with no user input would not be passed.

Now, if your checkout form contains a group of input controls per item, you can build on that approach by carefully naming your other controls:

<div>
<input type="checkbox" name="ProductID" value="1" />Product #1<br />
<input type="textbox" name="Product1_Quantity">
<input type="textbox" name="Product1_Color">
</div>

<div>
<input type="checkbox" name="ProductID" value="2" />Product #2<br />
<input type="textbox" name="Product2_Quantity">
<input type="textbox" name="Product2_Color">
</div>

<div>
<input type="checkbox" name="ProductID" value="3" />Product #3
<input type="textbox" name="Product3_Quantity">
<input type="textbox" name="Product3_Color">
</div>

Now, again, on the server you could parse the data in this way:

<%
  Dim intID
  Dim intQuantity
  Dim strColor

  For Each intID In Request.Form("ProductID")
    ' this is a selected item
    intQuantity = Request.Form("Product" & intID & "_Quantity")
    strColor = Request.Form("Product" & intID & "_Color")
  Next
%>

You'd be able to perform validation and other logic on each group of selected items in this manner.



来源:https://stackoverflow.com/questions/2839227/variable-number-of-input-fields-in-classic-asp-form

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!