Jump to content

Populate Text Field From Value Selected From Drop Down Menu


eggie
 Share

Recommended Posts

I tried googling, but wasn't able to find exactly what I was after.I have a database:names (name_id, name, address, phone)I have a form page:select value = name_idtextbox = nametextbox = addresstextbox = phoneOk, here is what I am trying to do. The select option is populated from the database. If a name is selected fromt eh drop down list, I'd like the appropreiate information to populate teh textboxes.Is this an ASP ability or better with Java???

Link to comment
Share on other sites

Unless it would require a huge amount of data (more than 10K - 20K of text?) you should use ASP to construct Javascript objects for all the DB information you'll need. They could be associative arrays, I suppose. Just print the objects right there in the Javascript.Then, yes, in response to an onchange event in the drop down, use Javascript to populate the text boxes. Since you have the objects right there, getting the correct values in the right boxes should require only few lines of code.If creating the Javascript objects would require a lot of text (slowing the initial download time) then the onchange event could trigger an AJAX request. Name, address, and phone is a very small amount of data, so the AJAX communication could happen very quickly.

Link to comment
Share on other sites

Would you be willing to help construct a sample script? I'm not very good at javascript (meaning, not at all).I was going to use this, but I am not sure how to populate the java function...

function onChange() {  var Current =	document.formName3.selectName3.selectedIndex;  document.formName3.currentText3.value =	document.formName3.selectName3.options[Current].text;  document.formName3.currentValue3.value =  document.formName3.selectName3.options[Current].value;}

<form name="formName3" onSubmit="return false;"><select name="selectName3" onChange="onChange()"><option value="Option 0">Entry 0<option value="Option 1">Entry 1</select><input name="resetName3" type="reset" value="Clear"><br><br>Current Text:<input name="currentText3" type="text" value="">Current Value:<input name="currentValue3" type="text" value=""></form>

Link to comment
Share on other sites

You're mostly doing fine. I would change the name of your function to something other than onChange. You never know what is going to happen when you use a word that Javascript uses for something else. Should should definitely add closing </option> tags to your options. I prefer to keep statements all on one line. I've seen scripts work with the line breaks, but the rules say not to do it.If it's not working, write back and sat what happens.

Link to comment
Share on other sites

You're mostly doing fine. I would change the name of your function to something other than onChange. You never know what is going to happen when you use a word that Javascript uses for something else. Should should definitely add closing </option> tags to your options. I prefer to keep statements all on one line. I've seen scripts work with the line breaks, but the rules say not to do it.If it's not working, write back and sat what happens.
Ok, I'll clean it up adn give it a try.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...