Javascript to Populate ASP Form Fields based on Dropdown Selection

I have a form where the customer would like a JavaScript array created so when the user selects a name from a dropdown box the associated fields on the form are populated from the array.  The only call to the SQL database would be when the form first loads to create the JavaScript array.

Here is the form:

Name:  This is the dropdown box
Age:      Textbox
Grade:  Textbox

I am assuming I use onchange to call a function to get the values but I am unclear on how to create the array or populate the textboxs on the form.  

I am somewhat new to JavaScript so code examples would be greatly appreaciated.

Thank you.
Who is Participating?
skinsfan99Connect With a Mentor Author Commented:
After some trial and error I figured out the code.
sivagnanam chandrakanthTechnical LeadCommented:
in select box write Onchange event to call javascript function and in js function find the equivalent age and grade value from array based on selected name field value
Note your array should be accessible from jS

see this example
skinsfan99Author Commented:
You description is accurate but I don't see that the example shows what I need.

I am looking for a code example of how to create a javascript array after doing a SQL select.  The onchange on the dropdown would populate text boxes from the array, not another dropdown box.  If your example could be modified to show that solution maybe it will work but as I mention I am new to JavaScript.

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

sivagnanam chandrakanthTechnical LeadCommented:
Example for creating JS array from sql query

use indexOf function to find the equivalent value in age array and grade array
and fill values using

document.getElementById(age).value='equivalent value from array'
skinsfan99Author Commented:
I am using Classic ASP not ASP.NET.  I should have been more clear about that.

Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
I'm glad you figured it out.  Just remember the client side code (javascript) loads on the page last.  Your asp/vb code gets processed before the page loads, then html and js gets written to the page and finally the js code gets processed.

With this in mind, if you want to end up with generated code like below

var mycars = new Array();
mycars[0] = "Saab";
mycars[1] = "Volvo";
mycars[2] = "BMW";

but the data is in your database, you can generate it with your serverside code like

jsCode=jsCode&"var mycars = new Array();"
jsCode=jsCode&"mycars[0] = ""Saab"";"
jsCode=jsCode&"mycars[1] = ""Volvo"";"
jsCode=jsCode&"mycars[2] = ""BMW"";"
response.write jsCode
skinsfan99Author Commented:
Feedback provided did not solve the issue.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.