Link to home
Start Free TrialLog in
Avatar of Don Laucirica
Don LauciricaFlag for United States of America

asked on

ASP-Javascript array help needed.

I'm writing a travel expense calculator for our budget group.  I've a database of locations linked to counties linked to continents.  I would like to load the three tables into arrays for use by three drop-down select boxes in a form.  Choosing a continent would load the next drop-down box with countries, from which when a country is chosen those country’s locations are available in the third drop-down box, which is the actual field that is required for the travel calculator.  Right now it works but as a continent or country is chosen upon submission the ASP page reloads itself, loading the requested country or location choices.  I would like to do this without reloading the page, I imagine using javascript.  

Note: I got this to work with hard-coded arrays, I want to be able to call a database and have the code load an array for each drop-down box, without reloading the page.

If there is an article, book, web link discussing loading database info into a ASP, Javascript array(s) for use with drop-downs I would appreciate it.

Thanks Don L.
Avatar of COBOLdinosaur
COBOLdinosaur
Flag of Canada image

You can't go to the database without reloading the page.  What you can do is use a hidden frame and reload that. Then generate the code for the select after the data is received back from the database.  

However you may have performance problems with this for user with a slow connection. You might want to preload the data into the hidden frame unless it is a really huge amount of data.
 
Cd&
Avatar of Don Laucirica

ASKER

I was hoping to load the three tables (Continents, Counties & Locations) at the start, and then use javascript to rebuild the drop-down boxes based on either what continent or country was chosen.  - Therefore not requiring either a call the the database or reload of the page.
I was hoping to load the three tables (Continents, Counties & Locations) at the start, and then use javascript to rebuild the drop-down boxes based on either what continent or country was chosen.  - Therefore not requiring either a call the the database or reload of the page.
Avatar of webwoman
webwoman

Then you're going to have to load EVERYTHING with that first call to the database. Every possible option will need to be in a javascript array of some kind if you want to do it client side.

You COULD use frames for this -- while you'd still make the calls to the database, each would only load ONE page and a limited amount of data. And you changing your mind would be fast, because the first set of choices would never have to change. Selecting one would load the second frame and reset the third (which could be a straight HTML page). Selecting something from the second would change the third only. It would be flexible because you could pick and choose pretty easily. Keep the first choice, change the second a dozen times -- while it would be a call back to the database, it would be reloading only one small page (most likely only text) with a limited amount of data.  

The user wouldn't necessarily see what was going on. They could only bookmark the main frameset, which would probably be helpful. YOu could carry over values from the other frames and have the last frame load up a submit button, so you could send the info on to something else.

If you've got tons of data to include in that javascript array, this might be the way to go.
I would like to use this function on which builds a drop-down with a list from array (Secondlist) matched from what was chosen form list1.  The problem I am having is getting a ASP called database or a hard coded array integrated into the javascript. ** How do you incorporate ASP (VBscript SQL calls) into javascript - or how/can make calls on a database using javascript?

function setList(f) {
 secondlist=eval(f.list1.options[f.list1.selectedIndex].value);
 f.list2.options.length=0;
 for(i=0;i<secondlist.length;i++) {
  f.list2.options[i]=new Option(secondlist[i]);
 };
  f.list2.selectedIndex=0;
};
The frames option I understand.  I have been trying to avoid using frames.

Don L.
Client side JavaScript cannot make database calls.  It can only access the data that is on the page at load time. If you have to go back to the database, you have to go to a server side script and you get a reload.  

An HTML page does not hold an open pipe, so every call back to the server results in a reload of some sort.

You may be possible with some kind of activeX control to do the data transfer seamlessly, but you cannot access the database with a JavaScript function on the page.

Cd&
Like WW said, you have to load everything at once, then just use javascript to pull the correct array and display it as a select.


BRUNO
ASKER CERTIFIED SOLUTION
Avatar of webwoman
webwoman

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Got it. You've both answered what I was coming to realize over the last week or so. - Calling and loading large tables into memory is a method, but slows and sometimes binds-up the client as it resorts the loaded data rebuilding drop-downs on the page.  I've avoided/eliminated using frames as I've learned other methods but agree this is certainly the easier method if not the most efficient.

Both of you have either directly or indirectly provided me great insight.  I'm giving 'webwoman' the points.  Bruno, I'll post another 100 pts for you.

Finally, I'm still interested in Javascript database methods.  I've got the O'reilly "Javascript" definitive guide.  Are there any other books, articles, web sites that have info on the subject.

Thanks

Don L.
A lot depends on how many items you have to deal with -- if it's thousands and thousands of pieces of data and complex javascript arrays, it's going to take forever on the client. The server won't get hit, but given that the server normally is much more robust than most client machines would be, going back to the server can be faster than rerendering the entire page every time they want to see a new option.

But for smaller amounts of data and overloaded/limited servers, doing the rerendering client side takes a load off the server.

Like always, there's more than one way to do everything... Thanks for the A!