4532066
4532066

Reputation: 2110

Dynamic variables in a loop in Classic ASP

I have a simple form which takes details from a database of records which don't have a title on the associated description table.

I select the records via this simple SQL select:

    SELECT zz_item.photoID 
      FROM zz_item JOIN j_p3 ON zz_item.photoID = j_p3.photoID
 LEFT JOIN zz_description ON zz_item.photoID = zz_description.photoID 
      WHERE zz_item.photoShow = 1 
        AND zz_description.photoTitle IS NULL 
   ORDER BY zz_item.photoID DESC 
      LIMIT 2;

This returns recrods without a title.

I then loop through the records and for the purposes of this example, display 2 records which don't have a title.

The form data looks like this:

<form action="load-photos-info.asp" method="post">

    <input type="hidden" name="process" value="go" />
    <input type="hidden" name="MyIDs" value="2082,2081" />

    <input type="hidden" name="id-2082" value="2082" />
    <input class="form-control" type="text" name="title-2082" />
    <textarea class="form-control" name="description-2082" /></textarea>

    <input type="hidden" name="id-2081" value="2081" />
    <input class="form-control" type="text" name="title-2081" />
    <textarea class="form-control" name="description-2081" /></textarea>

    <button type="submit" id="doForm" class="btn btn-success">Save</button>
</form>

I hold the IDs to process in the MyIDs hidden form field.

When the form is posted, I thought I could loop through the records by chopping up the MyIDs into separate records via:

<%
process = request.form("process")
if process = "go" then

    MyIDs = request.form("MyIDs")

    MyFormArray = Split(MyIDs, ",")

    For i = 0 to UBound(MyFormArray)

        LoopID = MyFormArray(i)
        response.write LoopID & "<hr>"

    Next

end if
%>

This works to prove that I can access the individual IDs, but I can't work out how I could use a loop or array or something, to access the title and description form field values for each individual ID.

I think I'd need to use a dynamic variable name - e.g. something like this for the first loop through:

title = request.form("title-2081")
descr = request.form("description-2081")

Then next time through the loop, the variables I'm accessing would have to be:

title = request.form("title-2082")
descr = request.form("description-2082")

Is that possible?

Upvotes: 0

Views: 2331

Answers (1)

Martha
Martha

Reputation: 3854

You can use variables in a Request.Form().

For i = 0 to UBound(MyFormArray)
    LoopID = Trim(MyFormArray(i))
    Title(i) = Request.Form("title-" & LoopID)
    Description(i) = Request.Form("description-" & LoopID)
Next

Upvotes: 2

Related Questions