Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 308
  • Last Modified:

Inserting multiple form values for each record with ASP

I have a form with four (4) different form fields for each record written from a database. There are approximately forty (40) records dynamically written to the page. All of the like form fields are named the same, with the values holding the record ID to distinguish them.

When I submit the form, how can I group the like form fields together so I can insert a new record? Code sample is below...

<form>
<table>

<tr>
  <td valign="baseline"><input type="checkbox" name="ggs_id" value="book31" class="Book Reviews" onClick="document.getElementById('divPage_Id.31').style.display=(this.checked?'block':'none');return checkCount(this, 5);">
  </td>
  <td><select name="selUsage" class="body">
        <option value="" selected>Select...</option>
        <option value="book31.Website">Website</option>
        <option value="book31.Bulletin">Bulletin</option>
      </select>
  </td>
  <td><select name="selAudience" class="body">
        <option value="" selected>Select...</option>
        <option value="book31.Church">Church</option>
        <option value="book31.Committee">Committee</option>
      </select>
  </td>
  <td><select name="selDistribution" class="body">
        <option value="" selected>Select...</option>
        <option value="book31.0-25">0-25</option>
        <option value="book31.25-100">25-100</option>
      </select>
  </td>
</tr>

<tr>
  <td valign="baseline"><input type="checkbox" name="ggs_id" value="book34" class="Book Reviews" onClick="document.getElementById('divPage_Id.34').style.display=(this.checked?'block':'none');return checkCount(this, 5);">
  </td>
  <td><select name="selUsage" class="body">
        <option value="" selected>Select...</option>
        <option value="book34.Website">Website</option>
        <option value="book34.Bulletin">Bulletin</option>
      </select>
  </td>
  <td><select name="selAudience" class="body">
        <option value="" selected>Select...</option>
        <option value="book34.Church">Church</option>
        <option value="book34.Committee">Committee</option>
      </select>
  </td>
  <td><select name="selDistribution" class="body">
        <option value="" selected>Select...</option>
        <option value="book34.0-25">0-25</option>
        <option value="book34.25-100">25-100</option>
      </select>
  </td>
</tr>

<tr>
  <td valign="baseline"><input type="checkbox" name="ggs_id" value="tape4" class="Tape" onClick="document.getElementById('divPage_Id.4').style.display=(this.checked?'block':'none');return checkCount(this, 5);">
  </td>
  <td><select name="selUsage" class="body">
        <option value="" selected>Select...</option>
        <option value="tape4.Website">Website</option>
        <option value="tape4.Bulletin">Bulletin</option>
      </select>
  </td>
  <td><select name="selAudience" class="body">
        <option value="" selected>Select...</option>
        <option value="tape4.Church">Church</option>
        <option value="tape4.Committee">Committee</option>
      </select>
  </td>
  <td><select name="selDistribution" class="body">
        <option value="" selected>Select...</option>
        <option value="tape4.0-25">0-25</option>
        <option value="tape4.25-100">25-100</option>
      </select>
  </td>
</tr>

<tr>
  <td valign="baseline"><input type="checkbox" name="ggs_id" value="tape3" class="Tape" onClick="document.getElementById('divPage_Id.3').style.display=(this.checked?'block':'none');return checkCount(this, 5);">
  </td>
  <td><select name="selUsage" class="body">
        <option value="" selected>Select...</option>
        <option value="tape3.Website">Website</option>
        <option value="tape3.Bulletin">Bulletin</option>
      </select>
  </td>
  <td><select name="selAudience" class="body">
        <option value="" selected>Select...</option>
        <option value="tape3.Church">Church</option>
        <option value="tape3.Committee">Committee</option>
      </select>
  </td>
  <td><select name="selDistribution" class="body">
        <option value="" selected>Select...</option>
        <option value="tape3.0-25">0-25</option>
        <option value="tape3.25-100">25-100</option>
      </select>
  </td>
</tr>

<input type="submit" value="Submit">
</table>
</form>
0
Ritchie Scott
Asked:
Ritchie Scott
  • 17
  • 14
1 Solution
 
Carl TawnSystems and Integration DeveloperCommented:
You could tag the ID onto the name for each input to distinguis them from each other ?
0
 
Ritchie ScottIT ManagerAuthor Commented:
Initially I had the ID as part of the name, but am having a hard time deciding what to with the variables once they are retrieved from the form post. I need to get them into a format that I can use to insert into a DB.
0
 
Carl TawnSystems and Integration DeveloperCommented:
What format do they need to be in ? Retrieving the values and inserting to the DB should be a fairly straight forward process.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ritchie ScottIT ManagerAuthor Commented:
Well, since there are around 40 potential records with 4 form field values each I need to find a way to group them together in an array or something I guess. Then I think I should be able to break the array aprt and split off the field names to get the values. Right now, I get stuff like this when I write out the request.form...

selFaqUsage.301 =
selFaqUsage.189 =
selFaqUsage.178 =
selFaqUsage.97 =
selEssayUsage.59 =
selEssayAudience.59 =
selEssayDistribution.59 =
selEssayUsage.184 =
selEssayAudience.184 =
selEssayDistribution.184 =
selEssayUsage.182 =
selEssayAudience.182 =
selEssayDistribution.182 =
0
 
Carl TawnSystems and Integration DeveloperCommented:
If you store all the IDs for your records in a field with the same name, they should get returned as a comma-seperated list. You can then split that and loop though using the IDs in the array to match up the correct form fields.
0
 
Ritchie ScottIT ManagerAuthor Commented:
The data is coming from different tables (i.e. books, tapes, etc.), so there is the possibility of having the same ID in each table. This is the reason for using a prefix or suffix in the naming convention. When I write the records to the DB, I plan on splitting the data up based on what type they are.

So if there are two tapes checked off on the form and one book, I will need to fill the following fields: Tape1_Id, Tape1_Usage, Tape1_Audience, Tape1_Distribution, Tape2_Id, Tape2_Usage, Tape2_Audience, Tape2_Distribution, Book1_Id, Book1_Usage, Book1_Audience, Book1_Distribution.

Make sense?
0
 
Carl TawnSystems and Integration DeveloperCommented:
Ah, ok.

In that case you may need a bit more of a complex naming convention. Maybe instead of just storing ID you store "TableName_ID_". You would then just have to parse the ID out of the string. Its a bit messy, but if you have a complicated form then you may not have many other options.
0
 
Ritchie ScottIT ManagerAuthor Commented:
Any suggestions for this issue? I thought about possibly passing a hidden form variable that would contain an array of the form selections containing item_id, usage, distribution, audience...

<input type="hidden" name="book.34" value="34,Website,25-100,Organization" />

Using this type of array which contains selections from the select boxes would require some JavaScript to populate the array based upon user selections. Any thoughts?
0
 
Carl TawnSystems and Integration DeveloperCommented:
Maybe prefixing the field names would be easier and storing a delimited list of all the table/id pairs in a hidden field:

<tr>
  <td valign="baseline"><input type="checkbox" name="ggs_id" value="book31" class="Book Reviews" onClick="document.getElementById('divPage_Id.31').style.display=(this.checked?'block':'none');return checkCount(this, 5);">
  </td>
  <td><select name="book31_selUsage" class="body">
        <option value="" selected>Select...</option>
        <option value="Website">Website</option>
        <option value="Bulletin">Bulletin</option>
      </select>
  </td>
  <td><select name="book31_selAudience" class="body">
        <option value="" selected>Select...</option>
        <option value="Church">Church</option>
        <option value="Committee">Committee</option>
      </select>
  </td>
  <td><select name="book31_selDistribution" class="body">
        <option value="" selected>Select...</option>
        <option value="0-25">0-25</option>
        <option value="25-100">25-100</option>
      </select>
  </td>
</tr>

That way you only need to append the prefix when retrieving the values from the form rather than having to trim them from the value. If I understand what you're trying to do of course ;o)
0
 
Ritchie ScottIT ManagerAuthor Commented:
So how would I group the form variables together when inserting the records to the database? I need to get the 4 form fields together to get them into the SQL statment.
0
 
Carl TawnSystems and Integration DeveloperCommented:
Say you had a hidden field with the table/id pairs:

    book_31, tape_13, book_2

The you use:

   arrID = Split(Request.Form("hdnIDs"), ",")

   For i = 0 To UBound(arrID)

        sID = Trim(arrID(i))
        sUsage = Request.Form(sID & "_selUsage")
        sAudience = Request.Form(sID & "_selAudience")
        sDistribution = Request.Form(sID & "_selDistribution")

        sID = Right(sID, Len(sID) - InStr(sID, "_"))       '// Parse out the numeric ID

        sSQL = "INSERT INTO YourTable (ID, Usage, Audience, Distribution) VALUES (" & sID & ",'" & sUsage & "','" & sAudience & "','" & sDistribution & "')"

   Next
0
 
Carl TawnSystems and Integration DeveloperCommented:
If you needed to use the Type to determine what table to use then you could just parse that out of sID as well.
0
 
Ritchie ScottIT ManagerAuthor Commented:
So how does the hidden field get populated with the values?
0
 
Carl TawnSystems and Integration DeveloperCommented:
That depends on how you are rendering your page.

Presumably you have to build up the name/id pairs as you output the form initially, so just add each one to a delimited list and output a hidden field at the end:

   While Not rs.EOF          '// Assuming this is one of the tables you are displaying

        sPrefix = "Tape_" & rs("ID")
        sIDList = sIDList & sPrefix & ","

        '// Code to output the fields

   Wend

   '// After outputting all of your tables
   sIDList = Left(sIDList, Len(sIDList) - 1)
   
   Response.Write "<input type=""hidden"" name=""IDList"" value=""" & sIDList & """ />"
0
 
Ritchie ScottIT ManagerAuthor Commented:
carl_tawn,

I appreciate your comments, but I'm not sure I am following you completely. I am pulling data from 4 different tables. I have the ID for each record being set as the checbox value. I then have 3 form fields for each record that can be checked by the user.

When I submit the form, I need to grab the ID from the fields that have the checkbox checked and the 3 form fields that correspond to the checkbox.

When the form posts, I need to grab the submitted values and write them to the DB. Each record written to the DB will contain the ID, Usage, Audience and Distribution.

I don't quite see how the above can be put together to make this happen. Any clarification you give is most appreciated.
0
 
Carl TawnSystems and Integration DeveloperCommented:
Ok, slight revision to that last post then. Lets break it down into steps:

1) Because all of your checkboxes have the same name you will get a comma-seperated list of all the selected options:

    sSelected = Request.Form("gg_ids")       '// Will currently contain something like:        book31, book34, tape4

2) Can you change the values of your checkboxes ? So instead of:

    <input type="checkbox" name="ggs_id" value="book31" class="Book Reviews" onClick="document.getElementById('divPage_Id.31').style.display=(this.checked?'block':'none');return checkCount(this, 5);">

You have:

    <input type="checkbox" name="ggs_id" value="book_31_" class="Book Reviews" onClick="document.getElementById('divPage_Id.31').style.display=(this.checked?'block':'none');return checkCount(this, 5);">

This would make it easier to seperate the ID from the table (i.e. get 31 as the ID)

3) If you change the names of you other options and the values from:

    <td>
        <select name="selUsage" class="body">
          <option value="" selected>Select...</option>
          <option value="book31.Website">Website</option>
          <option value="book31.Bulletin">Bulletin</option>
        </select>
    </td>

To:

    <td>
        <select name="book_31_selUsage" class="body">
          <option value="" selected>Select...</option>
          <option value="Website">Website</option>
          <option value="Bulletin">Bulletin</option>
        </select>
    </td>

Then you don't need to parse out the table/id from the value (I assume you only want to insert "Website" into the database rather than "book31.Website")

If you can ok/reject these changes then I can try to explain how it all hangs together.
0
 
Ritchie ScottIT ManagerAuthor Commented:
Changes are good. No problems renaming fields, since they are all being written to the page dynamically.
0
 
Carl TawnSystems and Integration DeveloperCommented:
Ok, good.

So once that is done you simply split the list of IDs from your checkboxes:

    arrSelected = Split(Request.Form("gg_ids"), ",")

    '// Now you can use each element in the array to grab the four field values from the Form
    For i = 0 To UBound(arrSelected)

        sVal = Trim(arrSelected(i))                                                    '// Retrieves   "book_31_"
        sUsage = Request.Form(sID & "_selUsage")                             '// Gets value from field named "book_31_selUsage"
        sAudience = Request.Form(sID & "_selAudience")
        sDistribution = Request.Form(sID & "_selDistribution")

       sID = Right(sVal, Len(sVal) - InStr(sVal, "_"))                           '// Retrieves "31_"
       sID = Left(sID, Len(sID) - 1)                                                    '// Retrieves "31", the ID for the record

       sTable = Left(sVal, InStr(sVal, "_") - 1)                                         '// Retrieves "book" if you need it to identify the table

       '// Build and execute your query

    Next


Hopefully that makes sense.
0
 
Ritchie ScottIT ManagerAuthor Commented:
Definitely makes more sense than before...I will try constructing something in the morning and do some testing. Thanks for you help and patience.
0
 
Ritchie ScottIT ManagerAuthor Commented:
carl_tawn,

I am trying to verifiy what is coming from the form with the following code, but I get nothing. Should the following produce valid data?

if Request.Form ("Submit") <> "" then
     dim arrSelected, sVal, sUsage, sAudience, sDistribution, sID, sTable, i
     arrSelected = Split(Request.Form("gg_ids"), ",")

    For i = 0 To UBound(arrSelected)
          sVal = Trim(arrSelected(i))                                          
          sUsage = Request.Form(sID & "_selUsage")                              
          sAudience = Request.Form(sID & "_selAudience")
          sDistribution = Request.Form(sID & "_selDistribution")      
          sID = Right(sVal, Len(sVal) - InStr(sVal, "_"))                        
          sID = Left(sID, Len(sID) - 1)                                          
          sTable = Left(sVal, InStr(sVal, "_") - 1)                    
      
          response.write "sVal = " & sVal & "<br>"
          response.write "sUsage = " & sUsage & "<br>"
          response.write "sAudience = " & sAudience & "<br>"
          response.write "sDistribution = " & sDistribution & "<br>"
          response.write "sID = " & sID & "<br>"
          response.write "sTable = " & sTable & "<br>"
          Next
end if
0
 
Carl TawnSystems and Integration DeveloperCommented:
You mean you get nothing at all, or you get the "sVal = " part with nothing after it ?
0
 
Ritchie ScottIT ManagerAuthor Commented:
I get nothing at all for some reason. The page loads, but nothing gets written to the page.

The following writes "BLUE", but nothing else.

if Len(Request.Form ("Submit")) > 0 then
     Response.Write "BLUE"

     dim arrSelected, sVal, sUsage, sAudience, sDistribution, sID, sTable, i
     arrSelected = Split(Request.Form("gg_ids"), ",")

    For i = 0 To UBound(arrSelected)
          sVal = Trim(arrSelected(i))                                          
          sUsage = Request.Form(sID & "_selUsage")                              
          sAudience = Request.Form(sID & "_selAudience")
          sDistribution = Request.Form(sID & "_selDistribution")    
          sID = Right(sVal, Len(sVal) - InStr(sVal, "_"))                        
          sID = Left(sID, Len(sID) - 1)                                          
          sTable = Left(sVal, InStr(sVal, "_") - 1)                    
     
          response.write "sVal = " & sVal & "<br>"
          response.write "sUsage = " & sUsage & "<br>"
          response.write "sAudience = " & sAudience & "<br>"
          response.write "sDistribution = " & sDistribution & "<br>"
          response.write "sID = " & sID & "<br>"
          response.write "sTable = " & sTable & "<br>"
          Next
end if

Any thoughts?
0
 
Carl TawnSystems and Integration DeveloperCommented:
Then that suggests that the array is empty. Add the following after Response.Write "BLUE":

    Response.Write Request.Form("gg_ids") & "<br/>": Response.Flush
    Response.Write "Elements: " & UBound(arrSelected) & "<br/>": Response.Flush

And see what you get.
0
 
Ritchie ScottIT ManagerAuthor Commented:
This is what I get...

Microsoft VBScript runtime error '800a000d'

Type mismatch: 'UBound'

/syndicate/selections.asp, line 878
0
 
Ritchie ScottIT ManagerAuthor Commented:
I moved it down a bit and got:

Elements: -1
0
 
Carl TawnSystems and Integration DeveloperCommented:
What did you get for the line:

   Response.Write Request.Form("gg_ids") & "<br/>": Response.Flush


?
0
 
Ritchie ScottIT ManagerAuthor Commented:
Nothing. Nothing displayed on the page.
0
 
Carl TawnSystems and Integration DeveloperCommented:
Doh, that because it should say:

    Response.Write Request.Form("ggs_id") & "<br/>": Response.Flush
0
 
Ritchie ScottIT ManagerAuthor Commented:
ok.

book_31_, book_30_, book_28_
Elements: -1
0
 
Ritchie ScottIT ManagerAuthor Commented:
I changed some stuff below, but get good stuff now...

if Len(Request.Form ("Submit")) > 0 then

      dim arrSelected, sVal, sUsage, sAudience, sDistribution, sID, sTable, i
      arrSelected = Split(Request.Form("ggs_id"), ",")

      For i = 0 To UBound(arrSelected)

      sVal = Trim(arrSelected(i))                                  
      sID = Right(sVal, Len(sVal) - InStr(sVal, "_"))              
      sID = Left(sID, Len(sID) - 1)
      sTable = Left(sVal, InStr(sVal, "_") - 1)
      sUsage = Request.Form(sTable & "_" & sID & "_selUsage")                    
      sAudience = Request.Form(sTable & "_" & sID & "_selAudience")
      sDistribution = Request.Form(sTable & "_" & sID & "_selDistribution")                                       
                              

                   response.write "sID = " & sID & "<br>"
         response.write "sVal = " & sVal & "<br>"
         response.write "sUsage = " & sUsage & "<br>"
         response.write "sAudience = " & sAudience & "<br>"
         response.write "sDistribution = " & sDistribution & "<br>"
         response.write "sTable = " & sTable & "<br>"
                Next
end if
0
 
Ritchie ScottIT ManagerAuthor Commented:
Forgot the good stuff..

sID = 31
sVal = book_31_
sUsage = Website
sAudience = Committee
sDistribution = 0-25
sTable = book
sID = 30
sVal = book_30_
sUsage = Print Handout
sAudience = Other
sDistribution = 1000-5000
sTable = book

I believe this is exactly what I need. Thanks for your help carl_tawn!!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 17
  • 14
Tackle projects and never again get stuck behind a technical roadblock.
Join Now