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

Forms and Array

Status
Not open for further replies.

gazza52

Technical User
Nov 8, 2005
5
0
0
GB
Hey,

I have a HTML form which has drop down boxes and a number of values.

When the submit button is pressed I would like whatever value is in the drop down box to be passed to an array.

For example

Drop Down Menu Box 1 Value selected "Ferrari"
Drop Down Menu Box 2 Value selected "Mercedes"
Drop Down Menu Box 3 Value selected "Ford"
Drop Down Menu Box 4 Value selected "VW"

When submit is pressed.. create a new array called car and pre-load values..

var car=new Array("Ferrari",
"Mercedes",
"Ford",
"VW"

)
 
For the first part add a call to your array adding function:
Code:
<script type="text/javascript">
var car=new Array();

function addToArray(dropdown){
car.push(dropdown.value);
}
</script>

<select onChange="addToArray(this);">
...
</select>

As you select cars are added to the car array.

The second part is not clear, as once you change to another page via the form submission, Any JS generated data is cleared so your array would have disappeared.

You could have the created array be turned into a string and added to a hidden form input so it would be available in your receiving script.

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

Behind the Web, Tips and Tricks for Web Development.
 
I've created the following code but when I select from the dropdown menu it wont add it to the array as the length of the array shows 0.

My code is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"<html xmlns=" <head>
<title></title>
<script type="text/javascript">

var effect=new Array();

function addToArray(effect)
{
effect.push(effect.value);
}
document.writeln(effect.length);
</script>
</head>
<body>
<select name="effect" onChange="addToArray(this);">
<option value="blur">Blur</option>
<option value="desaturate">Desaturate</option>
<option value="emboss">Emboss</option>
</select>
</body>
</html>
 
You can't have two variables named the same. Basic programming here.

effect and effect: How does it know which one you are referring to.

Change the name of one to something different.

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

Behind the Web, Tips and Tricks for Web Development.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top