Solved

Javascript to ASP.

Posted on 2006-06-26
13
289 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
  • 7
  • 6
13 Comments
 
LVL 3

Expert Comment

by:gugfuz
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

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

Expert Comment

by:gugfuz
Comment Utility
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
Comment Utility
can  you please give me an example of how the first one works?

thanks a lot!!
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 3

Expert Comment

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

Author Comment

by:mrperfect75000
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now