...

View Full Version : getting the value of selected item in a form



smithpe
07-03-2002, 09:04 PM
Hi

I am trying with no luck to retrieve the value of a selected item in a drop down menu. I need to get this value so I can pass it along in another form.

Here is the code for the first form



<SELECT NAME="attr_value1" >
<OPTION VALUE="1">1
<OPTION VALUE="2">2
<OPTION VALUE="3">3
<OPTION VALUE="4">4
<OPTION VALUE="5">5
<OPTION VALUE="6">6
<OPTION VALUE="7">7
<OPTION VALUE="8">8
</SELECT>

Any help will be greatly appreciated

Thanks

Paul

Spookster
07-03-2002, 09:09 PM
document.forms["yourformname"].elements["attr_value1"].options[document.forms["yourformname"].elements["attr_value1"].selectedIndex].value

smithpe
07-03-2002, 09:24 PM
Thanks

In other words something like this



<script language="JavaScript">
labelval=""
for (var usedattr=1; usedattr< maxlabel; usedattr++) {
if (usedattr==1) {
labelval=document.forms["multiattrib"].elements["attr_value(usedattr)"].options[document.forms["multiattrib"].elements["attr_value(usedattr)"].selectedIndex].value
else
labelval=document.forms["multiattrib"].elements["attr_value(usedattr)"].options[document.forms["multiattrib"].elements["attr_value(usedattr)"].selectedIndex].value }
}
</script>

adios
07-03-2002, 09:33 PM
You've got a bunch of stuff in there, without the HTML that generated it or a description of what you're trying to do. Hard to analyze...

var sel = document.form_name.select_name;
var selVal = sel[sel.selectedIndex].value;

smithpe
07-03-2002, 09:39 PM
I am trying to pull the value of the selected item from the drop down menu. There can be a total of up to 5

here is the code for the form


<SELECT NAME="attr_value<%= attr %>" >
<%
set mva_valueField = rsMVAttr("mva_value")
do while Not rsMVAttr.EOF
%>
<%= mscsPage.Option(mva_valueField.value, 0) & mva_valueField.value%>
<% rsMVAttr.MoveNext

loop
maxlabel=attr%>
if label<>"" then
label=label&","&attr_label(attr)
else
label=attr_label(attr)
end if
rsMVAttr.Close
%>
</SELECT>


Here is the script I am trying to use



<script language="JavaScript">
labelval=""
for (var usedattr=1; usedattr< maxlabel; usedattr++) {
if (usedattr==1) {
labelval=document.forms["multiattrib"].elements["attr_value(usedattr)"].options[document.forms["multiattrib"].elements["attr_value(usedattr)"].selectedIndex].value
else
labelval=document.forms["multiattrib"].elements["attr_value(usedattr)"].options[document.forms["multiattrib"].elements["attr_value(usedattr)"].selectedIndex].value }
}
</script>


Here is a link to the page.
http://www.uniquely-ukraine.com/uniquely/product.asp?dept%5Fid=1101&pf%5Fid=11011

whammy
07-04-2002, 01:23 AM
If you're trying to do it client side, just pass the value to a function, or whatever... here's the easiest way to get a dropdown menu's value - I'll use an alert box for an example:



<select name="blah" onchange="alert(this.options[this.selectedIndex].value)">
<option value="1">1</option>
<option value="2">2</option>
</select>

smithpe
07-04-2002, 01:45 AM
Hi

I have tried the OnChange method but if they select all of the default it won't work.

Paul

whammy
07-04-2002, 09:23 PM
Huh?

smithpe
07-04-2002, 10:22 PM
Ok let try to explain a little better what I am trying to do

I have a page with 2 forms on it. The first form can have up to 5 drop down menus with attributes of the item ordered.
here is the code for the drop down menus.


<TD><%= attr_label(attr) %>:</TD>
<TD>
<SELECT NAME="attr_value<%= attr %>" >
<%
set mva_valueField = rsMVAttr("mva_value")
do while Not rsMVAttr.EOF
%>
<%= mscsPage.Option(mva_valueField.value, 0) & mva_valueField.value%>
<% rsMVAttr.MoveNext

loop

maxlabel=attr%>
<!-- save number of attribs for paypal cart -->
<!-- get the attrib labels for paypal --><%
if label<>"" then
label=label&","&attr_label(attr)
else
label=attr_label(attr)
end if
rsMVAttr.Close
%>
</SELECT>
</TD>


I have a second form that calls a different page on submission, but I need the values of the selected attributes in the drop down menus of the first form. I have been able to use code similar to what was posted here to fire off an alert box when the selected items change. What I really need to do is when the second form is submitted is to grab the values of all the selected items in the drop down menus from the first form. Please feel free to look at the page I am working on at here is the page I am working on (http://www.uniquely-ukraine.com/uniquely/product.asp?dept%5Fid=1101&pf%5Fid=11011)
:thumbsup:

whammy
07-05-2002, 10:54 PM
This is an ASP page... all you need to do to get the values, first of all, is *not* use javascript - you might as well use .asp to its potential, since it's designed to do exactly what you want (pass information from one page to another, etc. etc. etc.):

<%
Dim attr_value1
attr_value1 = Request.Form("attr_value1")

Response.Write(attr_value1)
%>

Use the code above, and you'll see that you can very easily get the values of (any) dropdown on the next page using .asp.

Just do the same with each <select> elements' value on the previous page, and you can get them all just as easily.

P.S. This is a fairly basic thing in ASP, I would recommend some tutorials, here's a few food websites for them!:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iisref/html/psdk/asp/asps6hmb.asp
http://www.w3schools.com/asp
http://www.aspalliance.com
http://www.haneng.com
http://www.liquidrage.com

I would also do a bit of research into the differences between "client-side" and "server-side" - someone here had a really good article on that, but unfortunately I've since lost the link :(

Javascript *can* pass values from page to page (but it's more complicated) - but why use javascript when you have the ability to use ASP? :)

Hope that helps!

:)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum