• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 300
  • Last Modified:

Javascript to ASP.

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
mrperfect75000
Asked:
mrperfect75000
  • 7
  • 6
1 Solution
 
gugfuzCommented:
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
 
mrperfect75000Author Commented:
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
 
gugfuzCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mrperfect75000Author Commented:
How do i have it submited to my ASP code page?
0
 
gugfuzCommented:
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
 
mrperfect75000Author Commented:
can  you please give me an example of how the first one works?

thanks a lot!!
0
 
gugfuzCommented:
Can you post your ASP code? Then I'll make it work for you.
0
 
mrperfect75000Author Commented:
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
 
gugfuzCommented:
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
 
mrperfect75000Author Commented:
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
 
gugfuzCommented:
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
 
mrperfect75000Author Commented:
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
 
gugfuzCommented:
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

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now