Solved

Inserting multiple form values for each record with ASP

Posted on 2006-07-11
31
260 Views
Last Modified: 2008-03-03
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
Comment
Question by:Ritchie Scott
  • 17
  • 14
31 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17084216
You could tag the ID onto the name for each input to distinguis them from each other ?
0
 

Author Comment

by:Ritchie Scott
ID: 17084239
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17084287
What format do they need to be in ? Retrieving the values and inserting to the DB should be a fairly straight forward process.
0
 

Author Comment

by:Ritchie Scott
ID: 17084365
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17084406
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
 

Author Comment

by:Ritchie Scott
ID: 17084492
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17084522
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
 

Author Comment

by:Ritchie Scott
ID: 17090069
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17090183
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
 

Author Comment

by:Ritchie Scott
ID: 17090812
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17091083
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17091094
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
 

Author Comment

by:Ritchie Scott
ID: 17092749
So how does the hidden field get populated with the values?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17093380
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
 

Author Comment

by:Ritchie Scott
ID: 17094634
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17094755
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
 

Author Comment

by:Ritchie Scott
ID: 17094786
Changes are good. No problems renaming fields, since they are all being written to the page dynamically.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 17094895
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
 

Author Comment

by:Ritchie Scott
ID: 17094919
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
 

Author Comment

by:Ritchie Scott
ID: 17100326
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17100377
You mean you get nothing at all, or you get the "sVal = " part with nothing after it ?
0
 

Author Comment

by:Ritchie Scott
ID: 17102120
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17102245
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
 

Author Comment

by:Ritchie Scott
ID: 17102254
This is what I get...

Microsoft VBScript runtime error '800a000d'

Type mismatch: 'UBound'

/syndicate/selections.asp, line 878
0
 

Author Comment

by:Ritchie Scott
ID: 17102266
I moved it down a bit and got:

Elements: -1
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17102767
What did you get for the line:

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


?
0
 

Author Comment

by:Ritchie Scott
ID: 17102829
Nothing. Nothing displayed on the page.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17102980
Doh, that because it should say:

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

Author Comment

by:Ritchie Scott
ID: 17103312
ok.

book_31_, book_30_, book_28_
Elements: -1
0
 

Author Comment

by:Ritchie Scott
ID: 17103529
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
 

Author Comment

by:Ritchie Scott
ID: 17103546
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now