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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

select list option providing 2 values

Status
Not open for further replies.

struth

Programmer
Aug 26, 2001
114
GB
I need to have two values passed to the DB when a user selects an option from a dynamic drop down eg a school name and its ID. I can get the school name from:

<select name=&quot;School&quot; class=&quot;CSselectRed&quot;>
<%
While (NOT rsSchools.EOF)
%>
<option value=&quot;<%=(rsSchools.Fields.Item(&quot;SchoolName&quot;).Value)%>&quot; ><%=(rsSchools.Fields.Item(&quot;SchoolName&quot;).Value)%></option>
<%
rsSchools.MoveNext()
Wend
If (rsSchools.CursorType > 0) Then
rsSchools.MoveFirst
Else
rsSchools.Requery
End If
%>


But how can I pass the relevant school's ID to a hidden form field when the option is selected (which is the way I think it can be done)?

TIA
Struth

 
use javascript:
change your select tag to:

<select name=&quot;School&quot; class=&quot;CSselectRed&quot; onchange=&quot;document.YourFormName.HiddenFieldName.value=this.options[this.selectedIndex].text&quot;>


 
Thanks I tried this but I got

Microsoft OLE DB Provider for ODBC Drivers (0x80040E14)
[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression

My revised code was:


<!-- School Select box - value provides school name -->
<select name=&quot;School&quot; size=&quot;4&quot; class=&quot;CSselectRed&quot; onchange=&quot;document.form1.SchoolID.value=this.options[this.selectedIndex].text&quot;>
<option selected>Which is your school?</option>
<%
While (NOT rsSchools.EOF)
%>
<option value=&quot;<%=(rsSchools.Fields.Item(&quot;SchoolName&quot;).Value)%>&quot;><%=(rsSchools.Fields.Item(&quot;SchoolName&quot;).Value)%>
</option>
<%
rsSchools.MoveNext()
Wend
If (rsSchools.CursorType > 0) Then
rsSchools.MoveFirst
Else
rsSchools.Requery
End If
%>
</select>
<!-- End School select -->

<!-- SchoolID hidden txtbox - value provides school ID -->
<input name=&quot;SchoolID&quot; type=&quot;hidden&quot; value=&quot;<%=(rsSchools.Fields.Item(&quot;SchoolID&quot;).Value)%>&quot;>
<!-- End SchoolID hidden txtbox -->


I think what is happening is that the school name is being passed to schoolID instead of it generating the schoolID which relates to the chosen school name. The insert creates the error as it expects a numeric value.

Any more ideas?

Tia
Struth
 
arent you meant to have the school id in either the value or text of the option tag, you only have the school name.

im not sure if you have the id in your recordset but if you do your option tag code should look like this:

<option value=&quot;<%=(rsSchools.Fields.Item(&quot;SchoolID&quot;).Value)%>&quot;><%=(rsSchools.Fields.Item(&quot;SchoolName&quot;).Value)%>

then in you select tag you can write:

select name=&quot;School&quot; size=&quot;4&quot; class=&quot;CSselectRed&quot; onchange=&quot;PopulateHidden(this.value,this.options[this.selectedIndex].text)&quot;>

then write the follwoing script:

<script language=javscript>
function PopulateHidden(schoolID,schoolText)
{
document.form1.SchoolData.value=schoolID+&quot;:&quot;+schoolText;
}
</script>

where SchoolData is the name of your hidden field

in your asp to get the selected school name and id:

<%
dim arrSchoolData
dim strSchoolText
dim strSchoolID
arrSchoolData = split(request.form(&quot;SchoolData&quot;),&quot;:&quot;)
strSchoolID = arrSchoolData(0)
strSchoolText= arrSchoolData(1)
%>


hope this works

 
Mmm this really strikes me as getting close but I kept getting an error on strSchoolID = arrSchoolData(0). I moved it around but that line kept giving me problems.

I think I may have to work around this by popping in a session variable on the school ID which I can work off in later pages.

Thanks again
Struth
 
when you do a view source of the page what does the HTML for the select box look like?
 
Like this - with 114 options removed :)

<select name=&quot;SchoolID&quot; size=&quot;4&quot; class=&quot;CSselectRed&quot;>
<option selected value=&quot;&quot;>Which is your school?</option>
<option value=&quot;1&quot;>Ben Jonson</option>
<option value=&quot;116&quot;>Wellington</option>
</select>

Does this help?

Thanks Struth
 
the onchange event handler is not in the select tag, it should say:

<select name=&quot;SchoolID&quot; size=&quot;4&quot; class=&quot;CSselectRed&quot; onchange=&quot;PopulateHidden(this.value,this.options[this.selectedIndex].text)&quot;>
...
</select>

also add the hidden field after the </select> tag:
<input type=hidden name=&quot;SchoolData&quot;>


and you will also need to make sure the following script is there:

<script language=javscript>
function PopulateHidden(schoolID,schoolText)
{
document.form1.SchoolData.value=schoolID+&quot;:&quot;+schoolText;
}
</script>


this is probably the reason you are getting the errors
 
Sorry I had stripped it out. I replaced it and tried again but with the same results

ERROR:
Microsoft VBScript runtime (0x800A0009)
Subscript out of range: '[number: 0]'
TMPligemykz3o.asp, line 350

VIEW SOURCE
<select name=&quot;SchoolID&quot; size=&quot;4&quot; class=&quot;CSselectRed&quot; onchange=&quot;PopulateHidden(this.value,this.options[this.selectedIndex].text)&quot;>
<option selected value=&quot;&quot;>Which is your school?</option>
<option value=&quot;1&quot;>Ben Jonson</option>
<option value=&quot;116&quot;>Wellington</option>
</select>

And the script and line 350 are below (I would normally put the Javascript in the head area)

<script language=javscript>
function PopulateHidden(schoolID,schoolText)
{
document.form1.SchoolData.value=schoolID+&quot;:&quot;+schoolText;
}
</script>
<!-- School Select box - value provides school ID -->
<select name=&quot;SchoolID&quot; size=&quot;4&quot; class=&quot;CSselectRed&quot; onchange=&quot;PopulateHidden(this.value,this.options[this.selectedIndex].text)&quot;>
<option selected value=&quot;&quot;>Which is your school?</option>
<%
While (NOT rsSchools.EOF)
%>
<option value=&quot;<%=(rsSchools.Fields.Item(&quot;SchoolID&quot;).Value)%>&quot;><%=(rsSchools.Fields.Item(&quot;SchoolName&quot;).Value)%></option>
<%
rsSchools.MoveNext()
Wend
If (rsSchools.CursorType > 0) Then
rsSchools.MoveFirst
Else
rsSchools.Requery
End If
%>
</select>
<!-- End School select -->
<%
dim arrSchoolData
dim strSchoolText
dim strSchoolID
>>LINE350 arrSchoolData = split(request.form(&quot;SchoolData&quot;),&quot;:&quot;)
strSchoolID = arrSchoolData(0)
strSchoolText= arrSchoolData(1)
%>
<!-- SchoolDATA hidden txtbox - value provides school Name -->
<input name=&quot;SchoolData&quot; type=&quot;hidden&quot; value=&quot;&quot;>
<!-- End SchoolDATA hidden txtbox -->

What do you think?

Thanks again
Struth
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top