www.webdeveloper.com
Results 1 to 5 of 5

Thread: Please help me with passing an array to javascript

  1. #1
    Join Date
    Dec 2004
    Location
    Qld. Australia
    Posts
    8

    Please help me with passing an array to javascript

    I need some help. I set up an array with values in them and want to use them later in a javascript routine that will be invoked from a selection on a form.

    The array entry I want to use is called "suburbcount()" and the actual array number will equal that of "cityindex" in the javascript routine which is the "selectedIndex" value.

    However, I am not sure how I will set it up to pass through to the javascript routine or alternatively how I would define it as maybe a hidden element in the form. What I pass through now I know is incorrect as the "indexctr" has been reset to 0, however, it relates to the "selectedIndex" value at this point.

    Many thanks for any help.

    My code is as follows :

    *** My code for setting up the array ****



    Code:
    
    
    indexctr = 2
    do until indexctr = citycount
    	sqlstring = "select * from [location-area] where [city-town] = " & chr(34) & displaycity(indexctr) & chr(34) & " order by [area];"
    	call adorecordset.open(sqlstring,adoconnection,adopenstatic,adlockreadonly)
      	suburbcount(indexctr) = adorecordset.recordcount
    	adorecordset.close
    	indexctr = indexctr + 1
    loop
    
    *** Code for my form with some print statements to check array set up okay - which it is ***

    Code:
    
    <form name="selectcity" method="post">
    
    <!--Full Area SELECTION - (but only City / Town) -->
    
    	<%
    	indexctr = 0
    	do until indexctr = citycount
    	%>
    	<a><%=indexctr%> - <%=displaycity(indexctr)%> - <%=suburbcount(indexctr)%></a><br />
    	<%
    		indexctr = indexctr + 1
    	loop
    	indexctr = 0
    	%>
    	
    	
    	<a class="blueinformation">Detailed list of <%=statename%> Cities and Towns</a><br /><br />				
    	<select class="information" name="city" onchange="cityselection('<%=databaseid%>','<%=programid%>','<%=suburbcount(indexctr)%>')"><%=cityoption%></select>	
    
    <!--HIDDEN FORM VARS-->
    
    <input type="hidden" name="countryindex" value="<%=areadictionary.item("countryindex")%>"></input>
    <input type="hidden" name="databaseid" value="<%=databaseid%>"></input>
    <input type="hidden" name="suburb" value="<%=allsuburbs%>"></input>
    <input type="hidden" name="selectedindex" value=""></input>
    </form>
    
    *** javascript routine *****
    Code:
    
    function cityselection(databaseid,programid,suburbcount)
    {
    	var cityindex = document.selectcity.elements["city"].selectedIndex;
    	var selectedcity = document.selectcity.elements["city"].value;
    	if (cityindex == 0)
    		{
    		alert("Please Enter a Valid Option");
    		return;
    		}
    	if (suburbcount == 0)
    		{
    		document.selectcity.action = "testcity.asp?databaseid=" + databaseid + "&programid=" + programid + "&suburbindex=None" + "&cityindex=" + cityindex + "&suburbcount=" + suburbcount;
    		document.selectcity.submit();
    		}
    	else
    		{
    		document.selectcity.elements["selectedindex"].value = cityselection-2;
    		document.selectcity.action = "suburblist.asp?databaseid=" + databaseid + "&programid=" + programid;
    		document.selectcity.submit();
    		}
    }
    

  2. #2
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    The two technologies cannot communicate with eachother. You can, however, use ASP to write the array to the outpur as a JavaScript array.
    Code:
    '| In your JS function.
    var suburbCount(<%= UBound(suburbCount) %>);
    
    <% For i = 0 To UBound(suburbCount) %>
          suburbCount[<%= i %>] = "<%= suburbCount(i) %>";
    <% Next %>
    Something like that should work.

    Regards.

  3. #3
    Join Date
    Dec 2004
    Location
    Qld. Australia
    Posts
    8

    Question


    Thanks for your reply buntine. Unfortunately I am one "pretty slow Queenslander" that only knows basic javascript.

    I added your code into my javascript routine but also went to work on my javascript book to try to understand what was being done.

    My understanding is that I am running asp (code bounded by <% & %> while in the javascript routine - is this correct?

    is the code <%= UBound(suburbCount) %> the same as my suburbcount() array that I set up earlier in my code. I am not sure what "UBound" is? **** just found UBound in my Visual Basic book so reading up on. Question still stands, I cannot get it to work ****

    I have then altered my javascript code to use suburbCount(cityindex) for my "if else" section of code as it would be this array entry I want to look at.

    I have re-added my code below to get further feedback as I not only cannot get it to work, but don't fully understand even after diving into my books.

    Many thanks.

    **** changed javascript code ****

    Code:
    
    function cityselection(databaseid,programid)
    {
    	var cityindex = document.selectcity.elements["city"].selectedIndex;
    	var selectedcity = document.selectcity.elements["city"].value;
    	
    	var suburbCount(<%= UBound(suburbCount) %> );
    
    		<% For i = 0 To UBound(suburbCount) %>
    		      suburbCount[<%= i %>] = "<%= suburbCount(i) %>";
    		<% Next %>
    		
    	if (cityindex == 0)
    		{
    		alert("Please Enter a Valid Option");
    		return;
    		}
    		
    	if (suburbCount[cityindex] == 0)
    		{
    		document.selectcity.action = "testcity.asp?databaseid=" + databaseid + "&programid=" + programid + "&suburbindex=None" + "&cityindex=" + cityindex + "&suburbcount=" + suburbCount[cityindex];
    		document.selectcity.submit();
    		}
    	else
    		{
    		document.selectcity.elements["selectedindex"].value = cityselection-2;
    		document.selectcity.action = "suburblist.asp?databaseid=" + databaseid + "&programid=" + programid;
    		document.selectcity.submit();
    		}
    }
    
    Last edited by jbernie; 12-14-2004 at 10:22 PM.

  4. #4
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    The ASP code is actually executed before the JavaScript code is even recognised. The ASP code I wrote before is simply printing plain text to the output (which is your HTML and JavaScript source code).

    If you look at your HTML source code (Right click > View Source) you will see that the ASP code has generated some JavaScript, which is just an array.

    The UBound() function in VBScript will return the upper-bound index of the array. eg If the array contains 10 items, UBounds(arrayName) will return 9 (because indexing starts at 0) Do you understad?

    My JavaScript is a little rusty and I think I made mistake in the original code I gave you. You may have to replace the parentheses ( with braces [ when declaring the array and its size.
    Code:
    '| In your JS function.
    var suburbCount[<%= UBound(suburbCount) %>]; '| This is the only line I have changed.
    
    <% For i = 0 To UBound(suburbCount) %>
          suburbCount[<%= i %>] = "<%= suburbCount(i) %>";
    <% Next %>
    The preceding code is just generating a JavaScript array. This is the fastest way I can think of that will enable you to "pass" an array from ASP to Javascript.

    Do you understand all of this babble?

    Regards.
    Last edited by buntine; 12-15-2004 at 02:28 AM.

  5. #5
    Join Date
    Dec 2004
    Location
    Qld. Australia
    Posts
    8
    Yes I understand most of it now. Will have a good try at getting it going today.

    Many thanks, appreciated

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles