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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I pass an array of values from a form element 1

Status
Not open for further replies.

LyndonOHRC

Programmer
Sep 8, 2005
603
US
I'm wanting to pass a dynamic set of values to a function from the option elements of a form. I'm am having trouble with my first idea, but if there's a better approach I'm surely open to other ideas.

The original select options will be populated dynamically from a user maintained database. My idea was to structure the value of each option something like this:
Code:
<option value='"amount","region","","employer"'>Jones Inc.</option>

This would result in a form with 3 input elements with element 3 not being needed.
I tried the following just to get the properties into an array and display them before moving on to constructing my dynamic form:
Code:
function ItemSelectedHandler(){
	var e = document.getElementById(selectedGroup);
	var strLicenseType = e.options[e.selectedIndex].value;
	if (strLicenseType){
		var propertyArray=new Array(strLicenseType);
		document.getElementById("itemDataEntryRow").innerHTML=propertyArray[0]+' '+propertyArray[1]]+' '+propertyArray[2]]+' '+propertyArray[3];
	}else{
		document.getElementById("itemDataEntryRow").innerHTML='No Selection';
	}
}

This resulted in array[0] containing the complete string and, of course, the rest of them are undefined.

1. Is this a good concept to proceed with?
2. If so, how do I populate the array using the string I've described?

Thank you in advance!

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel. Covey
 
Personally, I would use Ajax to query the database for the necessary data after a selection has been made, rather than having each option value be a complex comma separated string. You are increasing the page size unnecessarily with data you may not even use. And exposing all this data to anyone.

However if that is not possible, and you want to turn your comma separated string into an array, the easiest way is to simply use the split function.

Code:
var propertyArray=strLicenseType.split(',');

With that said, your value only contains 3 elements, so your array elements will be [0], [1] , and [2]. That's 3 elements. So the propertyArray[3] is always going to be undefined. Remove it from your concatenation to avoid any undefined errors.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
With that said, your value only contains 3 elements, so your array elements will be [0], [1] , and [2]. That's 3 elements. So the propertyArray[3] is always going to be undefined.

There are 4 values, 1 of which is empty.

<option value='[red]"amount"[/red], [blue]"region"[/blue], [green]""[/green], "employer"'>

Would .split(',') still return only 3 values?

-Geates

 
No, it will return 4. I missed the empty string in the middle. Split breaks a part a string at a predefined limit, in this case the comma. So it will produce the empty value at the [2] position.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
That worked perfectly. I agree about this being a place for Ajax. I'll probably have questions in that forum soon. LOL

Note for future readers:

I changed
Code:
<option value='"amount","region","","employer"'>Jones Inc.</option>
to
Code:
<option value='amount,region,,employer'>Jones Inc.</option>
as the quotes were included in the array element values when using the .split(','); function.

Thanks for the umph-teenth time Phil!

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel. Covey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top