Using Selectedindex value for calculations

M

Matthew Ferri

I'm running Windows 2000 SP4 using Office 2000 SP3 to create a web page.

My goal is to use the selectedindex value from one drop-down list to compute
a value based upon the combination of multiple items as part of a data entry
form

Below are two program/script fragments from Q articles that highlight my
problem.


First is a program fragment from Q170495 that retrieves the value of the
dropdown menu item with the help of selectedindex. Unfortunately it is FP97
and I can not reproduce the functional equivalent of it with FP2000.

"FP97: Sample VBScript to Calculate Contents of Drop-Down Lists"

sub button1_onclick

' Create the form object.

dim theform
set theform = document.form1

' Return the value from the first drop-down menu.
' Drop-down text
' boxes return values as text.
' The Cint function converts text
' into an integer.

firstvalue = Cint(theform.drop1.options _
(theform.drop1.selectedindex).text)

secondvalue = Cint(theform.drop2.options _
(theform.drop2.selectedindex).text)

' Display the results in a text box.

theform.Text1.value = firstvalue + secondvalue

end sub


Second is a program fragment from Q197959.

"HOW TO: Use VBScript or JavaScript to Create Drop-Down Control
to Open Specific URL using FrontPage 2000"

<p>A drop down scripted URL list</p>

<form method="POST" name="SendMe">
<p><select name="DropIt" size="1">
<option> http://www.microsoft.com </option>
<option> http://www.msn.com </option>
<option> http://www.microsoft.com/frontpage </option>
<option> http://www.microsoft.com/support/frontpage </option>
</select> <input type="button" name="B1" value="Go">
<SCRIPT FOR="B1" EVENT="onClick" LANGUAGE="VBScript">
select case document.sendme.dropit.selectedindex
case 0
navigate("http://www.microsoft.com")
case 1
navigate("http://www.msn.com")
case 2
navigate("http://www.microsoft.com/frontpage")
case 3
navigate("http://www.microsoft.com/support/frontpage")
end select
</SCRIPT>
</form>

This second code fragment works perfectly within my web page so I know it
executes properly. I just want to add a line to assign the selectedindex
variable to a web page-wide variable that I can use in seperate item
calculations as shown in the first code example.

Every assignment statement I have tried will not capture the value of the
selectedindex variable to a web page wide variable I can use.

I just want to know if the value of ...selectedindex is 0 to 3.

For example,

Webpagewidevalue = document.sendme.dropit.selectedindex


Thanks in Advance,
Matthew
 
J

Jim Buyens

The statement you showed:

Webpagewidevalue = document.sendme.dropit.selectedindex

is almost correct. You just have to capitalize Index

Webpagewidevalue = document.sendme.dropit.selectedIndex

This presumes, of course, that:

o Your <form> tag contains name="sendme"
o The <select> tag that defines your drop-down list
contains name="dropit"


Jim Buyens
Microsoft FrontPage MVP
http://www.interlacken.com
Author of:
*----------------------------------------------------
|\---------------------------------------------------
|| Microsoft Office FrontPage 2003 Inside Out
||---------------------------------------------------
|| Web Database Development Step by Step .NET Edition
|| Microsoft FrontPage Version 2002 Inside Out
|| Faster Smarter Beginning Programming
|| (All from Microsoft Press)
|/---------------------------------------------------
*----------------------------------------------------

-----Original Message-----

I'm running Windows 2000 SP4 using Office 2000 SP3 to create a web page.

My goal is to use the selectedindex value from one drop- down list to compute
a value based upon the combination of multiple items as part of a data entry
form

Below are two program/script fragments from Q articles that highlight my
problem.


First is a program fragment from Q170495 that retrieves the value of the
dropdown menu item with the help of selectedindex. Unfortunately it is FP97
and I can not reproduce the functional equivalent of it with FP2000.

"FP97: Sample VBScript to Calculate Contents of Drop-Down Lists"

sub button1_onclick

' Create the form object.

dim theform
set theform = document.form1

' Return the value from the first drop-down menu.
' Drop-down text
' boxes return values as text.
' The Cint function converts text
' into an integer.

firstvalue = Cint(theform.drop1.options _
(theform.drop1.selectedindex).text)

secondvalue = Cint(theform.drop2.options _
(theform.drop2.selectedindex).text)

' Display the results in a text box.

theform.Text1.value = firstvalue + secondvalue

end sub


Second is a program fragment from Q197959.

"HOW TO: Use VBScript or JavaScript to Create Drop-Down Control
to Open Specific URL using FrontPage 2000"

<p>A drop down scripted URL list</p>

<form method="POST" name="SendMe">
<p><select name="DropIt" size="1">
<option> http://www.microsoft.com </option>
<option> http://www.msn.com </option>
<option> http://www.microsoft.com/frontpage
 
S

Skydolphin

I prefer javascript to do this but that's my preference. This would work I just tested it. Put the onchange call on the dropdown. onchange="doCalc();
<script language="javascript"
function doCalc(

var firstvalue = document.all("drop1")(document.all("drop1").selectedIndex).text

var secondvalue = document.all("drop2")(document.all("drop2").selectedIndex).text

document.all("Text1").value = parseInt(firstvalue) + parseInt(secondvalue)



</script><select size="1" name="drop1" onchange="doCalc();"><option value="1">1</option><option value="2">2</option></select><select size="1" name="drop2" onchange="doCalc();"><option value="1">1</option><option value="2">2</option></select><input type="text" name="Text1" value=""

Rhonda
 
M

Matthew Ferri

Jim, thanks for your help.

Unfortunately, I still have this problem with this routine.

I simplified the script I originally discussed at the end of my first
message to highlight that I still cannot pass the value of
"...selectedIndex" to a webpage wide variable.

I can show that the selectedindex value is working properly by selecting
case 0, 2 or 3, but I still cannot pass the simplified webpagewide variable
back to the rest of the page for multiple pull down menu item calculations
as discussed below.

Is it that I am not properly defining the "webpagewide" variable as public
someplace? I have tried numerous options to no avail. As you can see, the
"navigate" command will work inside this routine, but the "session" command
will not work. I commented it while I was trying to understand what would
work and what would not work.

Thanks again in advance.
Matthew


select case document.A431.dropit.selectedindex
case 0
navigate("http://www.microsoft.com")
case 1
webpagewide = 1
' Session("webpagewide") = 1
' navigate("http://www.msn.com")
case 2
navigate("http://www.microsoft.com/frontpage")
case 3
navigate("http://www.microsoft.com/support/frontpage")
end select
 
J

Jim Buyens

The selectedIndex property of a dropdown list is only available to
code running on the browser.

Session variables are only available to code running on the server.

Hence your problem.

What you *do* have on the server is Request("dropit"), which returns
the value of the currently-selected drop-down item when the visitor
submitted the form. So, if you had:

<select name="dropit">
<option value="1">Animal</option>
<option value="2">Vegetable</option>
<option value="3">Mineral</option>
</select>

Request("dropit") would return 1, 2, or 3, depending on which item
the visitor selected. So, the following code should work:

<select name="dropit">
<option value="1"
Animal</option>
<option value="2"
Vegetable</option>
<option value="3"
Mineral</option>
</select>
<input type="submit" name="btnSub" value="Submit">
<%
Function navigate (aUrl)
Response.Redirect(aUrl)
End Function

if request("btnSub") <> "" then
Session("webpagewide") = Request("dropit")
select case Request("dropit")
case "1"
navigate("http://www.msn.com")
case "2"
navigate("http://www.microsoft.com/frontpage")
case "3"
navigate("http://www.microsoft.com/support/frontpage")
case else
navigate("http://www.microsoft.com")
end select
end if
%>

Jim Buyens
Microsoft FrontPage MVP
http://www.interlacken.com
Author of:
*----------------------------------------------------
|\---------------------------------------------------
|| Microsoft Office FrontPage 2003 Inside Out
||---------------------------------------------------
|| Web Database Development Step by Step .NET Edition
|| Microsoft FrontPage Version 2002 Inside Out
|| Faster Smarter Beginning Programming
|| (All from Microsoft Press)
|/---------------------------------------------------
*----------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top