• asp and autogenerated form element

    Home » Forums » Developers, developers, developers » Web design and development » asp and autogenerated form element

    Author
    Topic
    #372980

    I am in the middle of constructing a shopping cart and have so far been able to pull a list of items from an Access table for display via .asp to the browser. in the course of trying to get the submit info to reflect the choices selected via an asp generated page, i noticed that the actual names and descriptions of my items are not form elements (text boxes, etc.) but HTML code — and as far as I know, you pass the name/value of the form element in a SUBMIT. So, I got asp to create a checkbox form element with the ItemID of each Title selected written to the checkboxes NAME element. Now I should be able to pass the checkbox to another form via SUBMIT, as it is a form element but, the problem now arises:

    I don’t know ahead of time which checkboxes will be coming thru on the SUBMIT button, so what I’d like to do is set up a For Each…Next loop to parse thru the pages’ form elements and display only those for which a checkbox has been clicked. So: is there an .asp method for finding all form elements in a page? Or is there something else that would work better? (I thought about javascript but not sure where to go after the word ‘javascript’ seeps thru my brain)

    asp 3.0+, 2000 Prof, Office 2000, Frontpage as editor, Access2000 as db source. trying to be platform-neutral but NS (-)4… may not apply.

    Viewing 0 reply threads
    Author
    Replies
    • #597936

      I may have lost you, but here goes: When a form is submitted via HTTP the value of a checkbox is only included in the data if the checkbox was checked at the time the form was submitted. So if your form has three checkbox elements (chk1, chk2, chk3), and the user checks only the first, then the Response.Form data includes only chk1. The value of chk1 is whatever you assigned as chk1’s value, or “on” if you didn’t specify a value for the checkbox element.

      In VB you can iterate through the submitted form elements using “For Each item In Request.Form … Next”. Here’s a debug script I use to look at submitted data. I’d give you a better example, but someone has borrowed my ASP book with VB examples.

      '//
      '// Show the contents of a form object returned in an HTTP Request
      '//
        Sub dumpFormData(objForm)
        
          If IsEmpty(objForm) Then
            Response.Write "
      
      No Form Data Present"
          Else
            Response.Write "
      
      ----- Start Form Data -----"
            For Each Item in objForm
              Response.Write "
      
      " & Item & ": "
              Response.Write objForm(Item) & "" & vbNewline
            Next
            Response.Write "
      
      ------ End Form Data ------"
          End If
        
        End Sub
      

      HTH

      • #597939

        thanks for the reply… actually i managed to get what i needed after a lot of searching around. since the checkbox NAME property is dynamically assigned, I can’t hard-code some response.form stuff to capture it. but i did find a method for referring to the checkbox in VBScript that does give the name, and worked that into a SQLString to pull records matching the choices selected:

        SQLString = “”

        ‘Display the Request.Form collection
        For Each checkbox In Request.Form
        sItemID = (checkbox)
        SQLString = SQLString & (checkbox) & “,”
        Response.Write(checkbox) & “,”
        Next

        Response.Write ”

        SQLString = Left(SQLString, Len(SQLString) -1)

        SQLString = “Select Title from Items where ItemID = ” & SQLString

        response.write SQLString

        ‘myRecordSet.Open SQLstring, conn, adOpenStatic

        I’m still hacking away at it, but the response.write string is:

        Select Title from Items where ItemID = 8,11,6,7

        which looks right to me. Now I have to get a table to display with the records found, but it’s late in the day..

      • #602019

        I can do something like this to enumerate *all* the field values submitted via a form but is there a way to grab just one field value rather than all of them? I need to have more resolution that just being able to list everything in the collection.

        for example, this will list all the field values from a SUBMIT, but I just want the *first* one, as it is my checkbox name:

        ‘Display the Request.Form collection

        For Each checkbox In Request.Form
        string = string & (checkbox) & “,”
        Response.Write(checkbox) & “,”
        Next

        As you may already know, the word ‘checkbox’ is just a tag; it could be anything but i’d *really* like it to be the darned checkbox. I could use this method if there was a way to step out of the For Each…Next when a condition is met, but I can’t seem to find the key word to break out of this kind of loop. OTOH, it feels cludgy to set up a loop just to break it, but if there’s no way to specify the checkbox independent of other items in the From SUBMIT collection, it would at least work.

      • #602033

        hey, i figured it out. you mentioned that it is an option to leave the value to “ON” so I changed that to the ItemID value from the db and named the value of the Checkbox “ItemID” and now it pulls the assigned value — which makes a lot more sense. so anyway, sorry to bother you but sometimes it takes a little blathering to sort out the neurons.

    Viewing 0 reply threads
    Reply To: asp and autogenerated form element

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: