Avatar of skinsfan99
skinsfan99Flag for United States of America

asked on 

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.
David
JavaScriptASP

Avatar of undefined
Last Comment
skinsfan99
Avatar of sivagnanam chandrakanth
sivagnanam chandrakanth
Flag of India image

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

http://stackoverflow.com/questions/13271027/javascript-filling-dropdown-from-array-based-on-another-value-of-another-dropdow
Avatar of skinsfan99
skinsfan99
Flag of United States of America image

ASKER

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.

Thanks
Example for creating JS array from sql query

http://aspdotnetcodebook.blogspot.in/2008/03/how-to-fill-javascript-array-from.html

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'
Avatar of skinsfan99
skinsfan99
Flag of United States of America image

ASKER

I am using Classic ASP not ASP.NET.  I should have been more clear about that.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of skinsfan99
skinsfan99
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

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

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

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

jsCode="<script>"
jsCode=jsCode&"var mycars = new Array();"
jsCode=jsCode&"mycars[0] = ""Saab"";"
jsCode=jsCode&"mycars[1] = ""Volvo"";"
jsCode=jsCode&"mycars[2] = ""BMW"";"
jsCode=jsCode&"</script>"
response.write jsCode
Avatar of skinsfan99
skinsfan99
Flag of United States of America image

ASKER

Feedback provided did not solve the issue.
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo