?
Solved

Javascript to ASP.

Posted on 2006-06-26
13
Medium Priority
?
297 Views
Last Modified: 2008-02-26
Please help!

I had this code in javascript but realised that part of the code has to be in ASP. As i read,  it is difficult to call a JS variable in ASP because of the client side/server side problem.

Can someone please give me the code in ASP?

I am trying to run a this java script code to grab a particular variable, venue, after the user drills down from country,  and pass it to the ASP code at the end of this page.

Choose site<br>

<form>

<select id = "sites">

<option selected>Select A site</option>
<option value = "www.blah.com">http://www.blah.com/</option>
<option value = "www.blah2.com">http://www.blah2.com/</option>


</select>
</form><br><br><br>

<form>
     <select id="countries" onchange="updatestates(this.selectedIndex)" style="width: 150px">
          <option selected>Select A Country</option>
          <option value="usa">USA</option>
          <option value="uk">United Kingdom</option>
     </select>
     
     <select id="states" onchange="updatecities(this.selectedIndex)" style="width: 150px">
     </select>

     <select id="cities" onchange="updatevenues(this.selectedIndex)" style="width: 150px">
     </select>
     
       <select id="venues" style="width: 150px">
     </select>
</form>

<script type="text/javascript">
<!--
var countrieslist = document.getElementById("countries");
var stateslist = document.getElementById("states");
var citieslist = document.getElementById("cities");
var venueslist = document.getElementById("venues");

var states = new Array()
states[0] = "";
states[1] = ["Select A State|", "California|california", "Texas|texas"];
states[2] = ["Select A State|", "Middlesex|middlesex", "Surrey|surrey"];

var cities = new Array()
cities[0] = "";

cities[1] = new Object;
cities[1][0] = "";
cities[1][1] = ["Select A City|", "San Francisco|san_francisco","Los Angeles|los_angeles"];
cities[1][2] = ["Select A City|", "Dallas|dallas","Houston|houston"];

cities[2] = new Object;
cities[2][0] = "";
cities[2][1] = ["Select A City|", "Teddington|teddington","Hampton|hampton"];
cities[2][2] = ["Select A City|", "Kingston|kingston","Guildford|guildford"];

var venues = new Array()
venues[0] = "";

venues[1] = new Object;
venues[1][0] = "";
venues[1][1] = new Object;
venues[1][1][0] = "";
venues[1][1][1] = ["Select A Venue|", "SF 1|sf_1","SF 2|sf_2"];
venues[1][1][2] = ["Select A Venue|", "LA 1|la_1","LA 2|la_2"];
venues[1][2] = new Object;
venues[1][2][0] = "";
venues[1][2][1] = ["Select A Venue|", "D 1|d_1","D 2|D_2"];
venues[1][2][2] = ["Select A Venue|", "H 1|h_1","H 2|h_2"];

venues[2] = new Object;
venues[2][0] = "";
venues[2][1] = new Object;
venues[2][1][0] = "";
venues[2][1][1] = ["Select A Venue|", "T 1|t_1","T 2|t_2"];
venues[2][1][2] = ["Select A Venue|", "H 1|h_1","H 2|h_2"];
venues[2][2] = new Object;
venues[2][2][0] = "";
venues[2][2][1] = "";
venues[2][2][2] = ["Select A Venue|", "K 1|k_1","K 2|k_2"];
venues[2][2][3] = ["Select A Venue|", "G 1|g_1","G 2|g_2"];

var country, state;

function updatestates(selectedcitygroup){
     country = selectedcitygroup;
     stateslist.options.length = 0;
     citieslist.options.length = 0;
     if(selectedcitygroup>0){
          for(i=0; i<states[selectedcitygroup].length; i++){
               stateslist.options[stateslist.options.length]=new Option(states[selectedcitygroup][i].split("|")[0], states[selectedcitygroup][i].split("|")[1])
          }
     }
}

function updatecities(selectedcitygroup){
      citieslist.options.length = 0;
      state = selectedcitygroup;
      venueslist.options.length = 0;
     if(selectedcitygroup>0){
          for(i=0; i<cities[country][selectedcitygroup].length; i++){
                  citieslist.options[citieslist.options.length]=new Option(cities[country][selectedcitygroup][i].split("|")[0], cities[country][selectedcitygroup][i].split("|")[1])
          }
     }
}

function updatevenues(selectedcitygroup){
      venueslist.options.length = 0;
      if(selectedcitygroup>0){
          for(i=0; i<venues[country][state][selectedcitygroup].length; i++){
               venueslist.options[venueslist.options.length]=new Option(venues[country][state][selectedcitygroup][i].split("|")[0], venues[country][state][selectedcitygroup][i].split("|")[1])
          }
     }
}
//-->
</script>



<%
dim site  //I want this to to be the variable "site" above // I probably dont have to delcare it again

dim venue // I want this to be variable "venues" above// I probably dont have to delcare it again



response.write("http://" &site & "/place.asp?venue=" & venues) //using the variables selected above, i will be able to use them here to build a url...

%>      



THanks!! I can split this into two if need be !!


0
Comment
Question by:mrperfect75000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 3

Expert Comment

by:gugfuz
ID: 16990798
Do you really need the ASP code? In your code you just use a response.write, what also exists in javascript (document.write).

If you really need the ASP, this is because you want the server to do something. This implies that the page is send to the server, *after* the user has done something. So your script is just fine in javascript, you have to submit the values to the server by a form or by AJAX.
0
 

Author Comment

by:mrperfect75000
ID: 16990866
yeah i really need ASP code as i have other parts i need to add to it.

Would be just very convinient in ASP
0
 
LVL 3

Expert Comment

by:gugfuz
ID: 16990894
Then I suggest to split the page, and to first show the form, let it be submitted to the server to your page with ASP code.

If you do not want the page been submitted, then post your values to the server in the background using AJAX, and then document.write the result in your page.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:mrperfect75000
ID: 16992217
How do i have it submited to my ASP code page?
0
 
LVL 3

Expert Comment

by:gugfuz
ID: 16992648
Indeed, set as action the url of your new asp page and add a submit burron on the bottom of your page.
In your ASP page you get the values by Request.Form('countries'), Request.Form('states'), and so on.


Or, make it so that your ASP page has as result/output just the line response.write("http://" &site & "/place.asp?venue=" & venues) , and put it at the bottom of your page with AJAX everytime the value of 'venues' changes.
0
 

Author Comment

by:mrperfect75000
ID: 16993272
can  you please give me an example of how the first one works?

thanks a lot!!
0
 
LVL 3

Expert Comment

by:gugfuz
ID: 16993635
Can you post your ASP code? Then I'll make it work for you.
0
 

Author Comment

by:mrperfect75000
ID: 16993881
this is the asp code i would eventually want to use to query DB and then insert use the results in the javascript i have above -

those select statements will eventually be gotten from the DB
Dim sConnStr, sRecords, adoDBConn, adoRecords

Set adoDBConn = Server.CreateObject("ADODB.Connection")
sConnStr "Provider=sqloledb;Data Source=Aron1;Initial Catalog=pubs;User Id=sa;Password=asdasd;"
'Replace Aron1 with your SQL Server 2000 hostname or IP address
'Replace pubs with the database where the table is stored
'Replace User ID with your user account (though you could use sa)
'Replace the password with your password

adoDBConn.Open sConnStr

sRecords = "SELECT fieldname FROM tablename WHERE fieldname2 = 'somevalue'"
Set adoRecords = adoDBConn.Execute(sRecords)
Response.Write "<select name=""mySelectField"">"
Do While Not adoRecords.EOF
      Response.Write "<option value=""" & adoRecords.Fields("fieldname") & """>" & adoRecords.Fieldname & "</option>"
Loop
Response.Write "</select>"




This is the eventual asp i want to use to generate the URL. My major problem is getting the javascript variables to work in asp, since asp is excuted first since its on the server side.



<%
dim site  //I want this to to be the variable "site" above // I probably dont have to delcare it again

dim venue // I want this to be variable "venues" above// I probably dont have to delcare it again



response.write("http://" &site & "/place.asp?venue=" & venues) //using the variables selected above, i will be able to use them here to build a url...

%>    






Any help will be greatly appreciated.  

thanks
0
 
LVL 3

Expert Comment

by:gugfuz
ID: 16994280
The link between the ASP code and the rest is not very clear.

Am I correct that you will get the values for the select boxes from the database?
Response.Write "<select name=""mySelectField"">"
  Do While Not adoRecords.EOF
        Response.Write "<option value=""" & adoRecords.Fields("fieldname") & """>" & adoRecords.Fieldname & "</option>"
  Loop
  Response.Write "</select>"


Then all this can be put in the same page, and you can create the link with javascript.
0
 

Author Comment

by:mrperfect75000
ID: 16994483
yes i want to get the values from the select boxes from a DB. What do you mean create link with java script?

Like i said , my problem is having asp recognize the java script variable, can you give me an example of how this can be done?


thanks for all your help
0
 
LVL 3

Expert Comment

by:gugfuz
ID: 16994937
I just do not understand why you need the ASP to create the link.

Add following function to the javascripts:
function createlink(){
      var elLinkDIV = document.getElementById('linkDIV');
      var elsite = document.getElementById('sites');
      var elvenue = document.getElementById('venues');
      elLinkDIV.innerHTML='<a href=\'http://' + elsite.options[elsite.selectedIndex].value + elvenue.options[elvenue.selectedIndex].value + '\'>link</a>';
}

Put an extra div on the place where you want the link to be shown (just bellow the /form tag?):
<div id="linkDIV"></div>

And to end, add an onchange event to the venues select box:
       <select id="venues" style="width: 150px" onchange="createlink();">


Is this wat you were looking for?
0
 

Author Comment

by:mrperfect75000
ID: 16996425
The reasons i wanted to use ASP are these....
Can i open a DB with javascript? I thought i could only do it with asp thats why i wanted to use asp to build the link and use asp to open the db, then i ran into the problem of variables with JS and ASP.

Now i dont mind just using JS. I can close this and open another question about opening a db with JS so that the information comes directly from the DB instead of hard coded.

Please let me know your thoughts about it - Thanks a lot and once i get your reply i will close this and give you the points.

Thanks!!
0
 
LVL 3

Accepted Solution

by:
gugfuz earned 2000 total points
ID: 16998858
As far as I can see you use the ASP to build the form:
  Response.Write "<select name=""mySelectField"">"
  Do While Not adoRecords.EOF
        Response.Write "<option value=""" & adoRecords.Fields("fieldname") & """>" & adoRecords.Fieldname & "</option>"
  Loop
  Response.Write "</select>"

Once build, you have a page with nothing but HTML and javascript, wich can work very well together.

Please correct me if I misunderstood.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question