Variable Number of input fields in Classic-ASP format

I have a checkout form where the number of products can be "n". So, how can I find out how many input fields are in the form and receive data from it?

thank

+3
source share
1 answer

If this is a group of individual controls - say, a variable number of checkboxes representing the elements - the solution is quite simple. 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 can do the following:

<%
  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
%>

, . 1 - n "FavoriteColor", For Each . .

, , , :

<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>

, , :

<%
  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
%>

.

+9

Source: https://habr.com/ru/post/1745547/


All Articles