Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dropdown selection populates a table row

Status
Not open for further replies.
Jun 24, 2002
19
0
0
US
I have been trying for days to do the following:

User selects an option from a dropdown (populated by SQL db). That selection would in turn display a table row with data properties accosicated with the dorpdown selection. Then we need to allow the user to select another option from the dropdown and have those properties added to the table in another row after the first selection, and on and on. This all needs to happen on the same page.

I have about a dozen pages with different coding attempts. So I'm not sure if I have any code to post that would help. I'm pretty sure any code I posted would be more confusing than helpful.

Any sort of ideas would help, basic concepts, code snippets, anything I'm up against a brick wall.

Thanks!!

Bob C. I/T Department WYSAC
 
Can you give an example of what your drop down menu looks like?

Do you want a user to select from a drop down which inserts a new record into your database?

BDC.
 
This code should help you. You need to create the javascript arrays in you asp code...

oldField = ""
arrayStr = ""
tempStr = ""
do while not rs.eof
if rs(&quot;mainField&quot;) <> oldField then
if len(tempStr) > 0 then
arrayStr = mid(tempStr,1) & &quot;)&quot; 'peel off first comma and close parenthesis
end if
selectStr = selectStr & &quot;,'&quot; & rs(&quot;mainField&quot;) & &quot;'&quot;
oldField = rs(&quot;mainField&quot;)
tempStr = vbcrlf & oldField & &quot;Arr = new Array(&quot;
end if
tempStr = tempStr & &quot;,'&quot; & rs(&quot;detailField&quot;) & &quot;'&quot;
rs.movenext
loop
if len(selectStr) > 0 then
selectStr = mid(selectStr, 1) 'peel off first comma
end if
if len(tempStr) > 0 then
arrayStr = mid(tempStr,1) & &quot;)&quot; 'peel off first comma and close parenthesis
end if


so your resulting HTML will look like this...
<script>
//selectArr = new Array(<%=selectStr%>) gives you the next line
selectArr = new Array(&quot;Breakfast&quot;,&quot;Lunch&quot;,&quot;Dinner&quot;)

//response.write (arrayStr) will give the following lines
BreakfastArr = new Array (&quot;Ham and Eggs&quot;,&quot;Pancakes&quot;,&quot;Waffles&quot;,&quot;Cereal And Toast&quot;)
LunchArr = new Array (&quot;BLT&quot;,&quot;Peanut Butter and Jelly&quot;,&quot;Grilled Cheese&quot;,&quot;Soup and Salad&quot;)
DinnerArr = new Array (&quot;Pizza&quot;,&quot;Tacos&quot;,&quot;Hamburgers&quot;,&quot;Fried Chicken&quot;,&quot;Grilled Salmon&quot;,&quot;Lasagna&quot;)

function setSelect(){
for (x=0; x<selectArr.length; x++){
if (! document.myForm.mySelect.options[x + 1]){
document.myForm.mySelect.options[x + 1] = new Option()
}
document.myForm.mySelect.options[x + 1].value = selectArr[x]
document.myForm.mySelect.options[x + 1].text = selectArr[x]
}
}

function showVals(){
selectVal = document.myForm.mySelect.options[document.myForm.mySelect.selectedIndex].value
valueArr = eval(selectVal + &quot;Arr&quot;)
tableNode = document.getElementById(&quot;displayTable&quot;)
newRow = document.createElement(&quot;tr&quot;)
for (x=0; x<valueArr.length; x++){
newTD = document.createElement(&quot;td&quot;)
newTD.innerHTML = valueArr[x]
newRow.appendChild(newTD)
}
tableNode.firstChild.appendChild(newRow)
}

</script>
<body onLoad=&quot;setSelect()&quot;>
<form name=&quot;myForm&quot;>
<select name=&quot;mySelect&quot; onChange=&quot;showVals()&quot;><option></select>
<br>
<table id=&quot;displayTable&quot; border=1>
</table>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook
 
BDC,

Dropdown code:
<select name=&quot;test_group&quot;>
<% While Not rsTestGroupGet.EOF %>
<option value=&quot;<%=rsTestGroupGet.Fields(&quot;test_group_id&quot;)%>&quot;<%If Request.Form(&quot;test_group&quot;) = rsTestGroupGet.Fields(&quot;test_group_id&quot;) Then Response.Write(&quot;selected&quot;)%>><%=rsTestGroupGet.Fields(&quot;test_group_abbr&quot;)%></option>
<% rsTestGroupGet.MoveNext
Wend%></select>

Table code:
<tr><td><%=Response.Write(rsSamples_TestGroupI.Fields(&quot;test_group_id&quot;))%></td></tr>
<tr><td><%=Response.Write(rsSamples_TestGroupI.Fields(&quot;test_group_abbr&quot;))%></td></tr>
<tr><td><%=Response.Write(rsSamples_TestGroupI.Fields(&quot;test_group_name&quot;))%></td></tr>
<tr><td><%=Response.Write(rsSamples_TestGroupI.Fields(&quot;lab_section_name&quot;))%></td></tr>
<tr><td><%=Response.Write(rsSamples_TestGroupI.Fields(&quot;sort_order&quot;))%></td></tr>

Now I don't know if this code will help in any way but you asked for some :)


dropdown:
test 1
test 2
test 3

Table:
Test# Test Name Test Group Sort#

User selects &quot;test 1&quot; and clicks &quot;add&quot; button. Test 1's attribuites are displayed in the table

test 1 (selected)

Test# Test Name Test Group Sort#
1 first test first group 1

Then user can select another test...

test 2 (selected)

Test# Test Name Test Group Sort#
1 first test first group 1
2 Second test second group 2

and on and on.

Now the dropdown is populated by the database and the attributes for the table are also called from the database.

At the end of it all the Selected tests are sent to a table in the database in the order finally listed in the table. It's just that simple right...

Thanks.

Bob C. I/T Department WYSAC
 
Sounds like you just need to remember what has been selected and in what order. How about writing the number of the option selected to a text file and appending the file with subsequent option numbers. So you will end up with a text file containing:

3
1
5

or delimited

3,1,5,

...for example.

You can use this to populate your table with every new selection and also to write to a new table in your database at the end.

You will need to use the filesystemobject for this. Read about it here:


Hope this helps,

BDC.
 
The code I wrote does not need you to call the server again to populate table rows. All data is sent to the HTML page and used (as needed) by the javascript code. Did the code I wrote above work for you?

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook
 
mwolf00

I can see how the code can work, I'm struggling with getting all my info in. (might come with time), My big question would be how does the selections made get written to the database?

Bob C. I/T Department WYSAC
 
To write the data to a database, you would have to store the selections and have the user submit them. I like the fact that each selection doesn't force a server call, but you may decide that having each selection entered into the db is more important than fewer server calls...

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top