PDA

Click to See Complete Forum and Search --> : What was selected in dropdown..


turfbult
Jan 9th, 2001, 11:45 PM
Hello,

How can I know what was selected from my dropdown menu, WITHOUT having to POST the page using a submit button.

I have a dropdown menu called lstCountry on a page called COUNTRY.ASP. When a country is chosen from lstcountry and submit is clicked STATE.ASP is loaded and within STATE.ASP I say strCountry = request.form ("lstcountry") - this works fine, but I want to know what country was selected BEFORE(!!) I load STATE.ASP, because depending on what Country was selected I want to load the next page.

T

Clunietp
Jan 9th, 2001, 11:52 PM
If you want to do this on the server side with ASP, you'll need to post your data to an intermediate page to determine where to send the data. You can also use client side javascript to do something like this for you. Which one do you prefer?

turfbult
Jan 10th, 2001, 12:26 AM
Clunietp,

I know more ASP then javascript, so I think I should go with ASP. What would you say is best??

I think I know what you're getting at...

After "submitting" from COUNTRY.ASP I'll go to an intermediate page (lets say TEST.ASP) and in TEST.ASP see what COUNTRY was selected and then load either CITY.ASP or STATE.ASP.

Am I on the right track??
Is this an "accepted" way of doing things??

T

[Edited by turfbult on 01-10-2001 at 01:31 AM]

turfbult
Jan 10th, 2001, 01:03 AM
Ok, what I did is this - keep in mind it's just a test to see if it works...

I created a page called REDIRECT.ASP and within this page I have the following code
<%
dim strCountry
strCountry = request.form("lstcountry") 'I get this from COUNTRY.ASP

if strCountry = "USA" then
response.redirect "city.asp"
else
response.redirect "state.asp"
end if
%>

And this is where my problem comes in....
Either page (CITY or STATE) that this page redirects to needs to know what COUNTRY was selected originally ie. strCountry or lstCountry and by just doing a RESPONSE.REDIRECT the variable is not "pulled thru". I used to handle this by using hidden inputs, but then a SUBMIT/POST must be executed!!

Steve tried to explain to me how to get around this, but I just dont get it.

PLEASE(!!!) anybody help. Getting this right is crucial to me.

Thanks,
T

Clunietp
Jan 10th, 2001, 10:25 AM
Hey Turf

if you MUST have this data submitted by a form, this can get really ugly really quickly (as opposed to the javascript version)

so I wrote you a javascript version that you can use on your main page to determine where to send the user


<html>
<head>
<script language="javascript">

function submitIt() {

// get country
var sCountry = new String(document.frm1.lstCountry.value.toUpperCase());
var sActionPage = new String();

// determine which page to send form to
if (sCountry == "USA") {
sActionPage = "page2.asp";
} else if (sCountry == "CANADA") {
sActionPage = "page3.asp";
}

// set form action
document.frm1.action = sActionPage;

// submit form
document.frm1.submit();

return;
}
</script>
</head>

<body>

<form name="frm1" method="post" action="">

<select name="lstCountry">
<option value="USA">USA
<option value="CANADA">CANADA
</select>

<input type="button" onclick="submitIt();" value="Submit">
</form>

</body>
</html>


If you don't like the javascript way, you can use the redirect page and send all of the form's data to the querystring, or you could create a hidden form page, which can get pretty nasty. I would personally use Javascript

Tom

turfbult
Jan 10th, 2001, 11:15 AM
Hey Tom,

This looks excellent - at last, someone that could REALLY help me!!

Just one more thing....

The reason why I want to redirect to a different page is because some COUNTRIES dont have STATES on my db. So I want to do a sql query to check my STATES table and if no STATES are found I want to go directly to my CITY.ASP page... something like

"select * from states where countrycode = '" & varcountry & "'"

'varcountry being what was selected in the dropdown

if rs.eof then
'load city.asp
else
'load states.asp
end if

How will I do this in javascript - can it be done??

Thank you very, very much for your effort!!!

T

Clunietp
Jan 10th, 2001, 09:59 PM
Hey Turf

You don't want to do this in Javascript because you can't (well, not easily)

This would be much better suited for ASP to do

"select * from states where countrycode = '" & varcountry & "'"

'varcountry being what was selected in the dropdown

if rs.eof then
Response.Redirect "city.asp"
else
Response.Redirect "states.asp"
end if

Whichever one that this page is supposed to handle, substitute the appropriate code instead of the response.redirect.

Note that if you plan on sending the user to a bunch of different URLs depending on conditions, you might want to switch to using querystrings because they are much easier to get and manipulate when submitting data to another page programmatically

turfbult
Jan 10th, 2001, 11:02 PM
Thanks Tom,

I think I'll have to start using querystrings, which means that I'll have to study again - I have never used them before.

Thanks for your help.
T

Clunietp
Jan 10th, 2001, 11:17 PM
Don't be intimidated, they're really no big deal, its just the extra data that is stuffed into the URL

like here at vbworld
http://www.vbforums.com/newreply.php?action=newreply&threadid=48229

the bolded stuff above is the querystring

then just use Request.Querystring("threadid") in ASP to get your data (in this case, that function would return the value 48229)


best of luck

Tom

turfbult
Jan 10th, 2001, 11:26 PM
I'll give it a try, because I've seen many website (like this one) using it.

One last thing....

Can I somehow manage to let met jscript "see" a variable defined within my ASP.

Ie.

<%
dim varMyVar
'here is all my asp code....
%>

<script language="javascript">

function submitIt()
{
//Can I now SOMEHOW use varMyvar in here???
}
</script>


T.

Clunietp
Jan 10th, 2001, 11:31 PM
sure, here ya go


<%@Language=VBScript EnableSessionState=False%>
<% option explicit

dim myVar

myVar = 7
%>
<html>
<head>
<script language="Javascript">

function sayHi() {
window.alert("The Value Is: " + <%= myVar %>);
}

</script>
</head>
<body onload="sayHi();">
Hi
</body>
</html>

turfbult
Jan 10th, 2001, 11:34 PM
Excellent, thanks a lot.

T

turfbult
Jan 11th, 2001, 12:09 AM
Tom,

I hope your still there and dont mind that I'm bothering you AGAIN.

I want to use this code you gave me earlier, but modify it that sCountry is only the first letter of what was selected.
Ie, if AUSTRALIA was selected then sCountry should only contain "A".

The reason why is because all COUNTRIES on my DB which do not have STATES has a "*" before the name, so I want to do something like......

if sCountry = "*" then
'go to city.asp
else
'go to state.asp
end if

function submitIt() {

// get country
var sCountry = new String(document.frm1.lstCountry.value.toUpperCase());
var sActionPage = new String();

// determine which page to send form to
if (sCountry == "USA") {
sActionPage = "page2.asp";
} else if (sCountry == "CANADA") {
sActionPage = "page3.asp";
}

// set form action
document.frm1.action = sActionPage;

// submit form
document.frm1.submit();

return;
}

Clunietp
Jan 11th, 2001, 12:16 AM
I think I know what you mean....

like this?

if (sCountry == "U") {
sActionPage = "page2.asp";
} else if (sCountry == "C") {
sActionPage = "page3.asp";
} else if (sCountry == "*") {
sActionPage = "myPage.asp";
}

You would have to specify a value of "*" for the option value in your select box

turfbult
Jan 11th, 2001, 12:21 AM
Yes, something like that, but remember that my dropdown menu gets filled by values from my db - using ASP, so COUNTRIES with no STATES automatically have the "*" before it and will be displayed in my dropdown as such, so all I want to check is whether the COUNTRY selected, has a "*" before it and if so, I'll do the if else. My only problem is that I only want the variable to be filled with the first letter (ie "*") and not with the whole string as it is doing now.

T

Clunietp
Jan 11th, 2001, 12:28 AM
gotcha

ok, all set, here ya go.

I'm off to bed now, hopefully this will do it for you tonight :)


<html>
<head>
<script language="javascript">

function submitIt() {

// get country
var sCountry = new String(document.frm1.lstCountry.value.substring(0,1).toUpperCase());

var sActionPage = new String();

// determine which page to send form to
if (sCountry == "U") {
sActionPage = "page2.asp";
} else if (sCountry == "C") {
sActionPage = "page3.asp";
} else if (sCountry == "*") {
sActionPage = "city.asp";
}

// set form action
document.frm1.action = sActionPage;

// submit form
document.frm1.submit();

return;
}
</script>
</head>

<body>

<form name="frm1" method="post" action="">

<select name="lstCountry">
<option value="USA">USA
<option value="CANADA">CANADA
<option value="*ISRAEL">ISRAEL
</select>

<input type="button" onclick="submitIt();" value="Submit">
</form>

</body>
</html>

turfbult
Jan 11th, 2001, 12:40 AM
It sure does Tom,

Once again, thanks for your help. You wont believe how much it is appreciated!!

T