#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    20
    Rep Power
    0

    Get rows (<tr>) and cells (<td> or <th>) from request.form


    I am building an application where the user can add or delete rows to/from an HTML table. Once they are happy, they then request that all the data get written to a database.

    This is done by calling an ASP (classic) routine.

    All the work about adding/deleting rows is all working fine in the HTML/Javascript page.

    Now I want to write all the data to the database via classic ASP.

    I can easily go through all the form.fieldnames in ASP.
    But I'm really perplexed about how to go through all the rows of the HTML table and get all the cells.
    Oh... there can be any number of rows, AND a varying number of columns (cells). Of course, there is a consistent number of cells for all rows for any execution.

    How can I tell how many rows there are in the table, and how many columns (cells) in each row ?

    It MUST be somewhere in the HTTPRequest, or ?????

    Thanks
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    20
    Rep Power
    0
    Well, I figured out a way to do this..... here is the explanation for anyone who might be interested.

    So to actually do the database update, I invoke an ASP module, from the html/javascript page.
    The ASP module needs to know all the values from the invoking form so it can do the database update.
    To invoke this ASP module, I am using XMLHTTPRequest.
    If you don't know what this is, or how it works, I advise you to do some research.
    Fundamentally it allows you to transfer control to another module (ASP, PHP, whatever). In so doing, it will NOT create a new window/page, but will allow it to process. And in the invocation, you can indicate if you wish to WAIT till it finishes (returns something), or carry on concurrently (it can still return some value).

    I use this capability quite extensively to build/construct dynamic page portions (DIVs) and then return them to the invoking routine.

    Anyway, I call the database update ASP module using this capability, and wait till it is finished it's work.
    But I need to get all the form fields to this module. (as per my original question)

    When you invoke a module via XMLHTTPRequest, you can pass it parameters. If you invoke it with a "GET" command, the parameters are visible to the user as part of the URL and appear in the address bar. But if you invoke it with a "POST" command, the parameters are hidden from the user. Essentially this is how the "request.form" function works.

    So, in my Javascript routine which invokes the ASP module, I created a bunch of logic to go through all the form fields, including the tables rows and columns, and built a parameter string of all these items, using a "name=value" pattern. Each "name=value" string was appended to the request after inserting a "&" as per standard parameter passing. So I essentially had a parameter string the looked like
    "&name1=value1&name2=value2&name3=value3......".

    Of course, the invoked ASP module needs to know the various names being provided. But for the table rows/cells I merely created arrays of comma-separated values. For example, I created an array for each row of cells. I gave that item a name of "row" + rownumber.
    Then in the ASP module, I merely looked for a field with a name that had "row" in it, then stored the value into a string, then parsed that string to remove the commas, and wind up with an array of values.

    I then construct the appropriate sql statements to update the database accordingly.

    It returns a message about the database being updated, which the invoking module then displays (alert) to the user. Remember it is waiting for the database update routing to finish/return.

    Everything now works perfectly.

    If anyone has any questions or comments, I'd be happy to respond.

    Cheers,
  4. #3
  5. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,449
    Rep Power
    4539
    Thanks for posting the update.
    ======
    Doug G
    ======
    Bartender to Rene Descartes "have another beer?" Descartes: "I think not" and he vanished.
    --Alfred Bester

IMN logo majestic logo threadwatch logo seochat tools logo