Solved

Javascript to Populate ASP Form Fields based on Dropdown Selection

Posted on 2013-01-06
7
524 Views
Last Modified: 2013-01-12
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
0
Comment
Question by:skinsfan99
  • 4
  • 2
7 Comments
 
LVL 12

Expert Comment

by:sivagnanam chandrakanth
ID: 38749815
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
0
 

Author Comment

by:skinsfan99
ID: 38749840
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
0
 
LVL 12

Expert Comment

by:sivagnanam chandrakanth
ID: 38749861
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'
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:skinsfan99
ID: 38749900
I am using Classic ASP not ASP.NET.  I should have been more clear about that.

Thanks
0
 

Accepted Solution

by:
skinsfan99 earned 0 total points
ID: 38750258
After some trial and error I figured out the code.
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 38750578
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
0
 

Author Closing Comment

by:skinsfan99
ID: 38769637
Feedback provided did not solve the issue.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now