Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto-fill fields from drop down option

Status
Not open for further replies.

bigcat48

Programmer
Aug 27, 2008
72
US
All:

This is what I want to happen:

1. When a user would fill out this form, they would first have to choose a company from a select menu preloaded with values.

2. Once the user has made a selection, somehow the form is generated with values from another table in the database based upon this selection.

I found a semi solution, but unfortunately one would have to manually load all values in the option value and hit "tab" to work. Also once manually loaded the fields have to be separated in order for the script to read each individual value. "-"

The only part I don't like is "manually loading the option value in the form".

It would be lovely if the option value would auto fill dynamically based upon what's in the db for that field.

Here's the semi-solution:
Code:
...
<head>
	<script>
	function fillProfileFields(){
		var Profile = document.getElementById("companyid");
		if(Profile.value.length > 0){
			var ProfileElements = Profile.value.split("-"); 
			document.getElementById("city").value = ProfileElements[0];
			document.getElementById("state").value = ProfileElements[1];
			document.getElementById("zip").value = ProfileElements[2];
			document.getElementById("overview").value = ProfileElements[3];
			document.getElementById("locations").value = ProfileElements[4];
		}
	}
	</script>
</head>

Here's the form part...
Code:
<span>
<select name="companyid" id="companyid" onChange='fillProfileFields();'>
						<option selected value="">-- select a company --</option>
						<option value="">---------</option>
						<option value="1-1-1-1-1-1-1-1-1-1-1-1-1">Company 1</option>
						<option value="2-2-2-2-2-2-2-2-2-2-2-2-2">Company 2</option>
						<option value="3-3-3-3-3-3-3-3-3-3-3-3-3">Company 3</option>					</select><label>Company Name</label></span>

the result in this example is all 1's.


Thanks in advance.

BG


 
I would use AJAX for this. PHP has a ready-made class (XAJAX) that does most of the work for you. A quick search for XAJAX in this forum will get you close, otherwise try forum1600 for AJAX-specific questions.

If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Check this out


It is a post I had answered too going back a couple of months. Like the johnwm, I suggest using XAJAX and I used it here.

You will find everything you need, short of the code specific to your DB ...

Hope this helps!


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top