Home: Perl Programming Help: Beginner:
Changing two values with one pull-down in a form?


Mar 6, 2001, 4:06 PM

Views: 337
Changing two values with one pull-down in a form?


I need to change two values in a form by using one pulldown-menu ... for example:

<select size="1" name="car">
<option value="blue car">blue car</option>
<option value="red car">red car</option>
<option value="yellow car">yellow car</option>

If anyone chooses "red car" the variable "price" should look like:

<input type="hidden" name="price" value="199">

if he chooses another model the content of "price" should change to a specific value ...

Any code-snippet for me?

Any help is appreciated!

User / Moderator

Mar 7, 2001, 12:50 PM

Views: 332
Re: Changing two values with one pull-down in a form?

If you want to change another value in the form you can have the select field use an 'onChange' option.

Here is an example that I use to set another select box and place a value into a text box...

<select name="sampleid" 
onChange="this.form.id.value = this.form.sampleid.options[this.form.sampleid.selectedIndex].value;
this.form.type.selectedIndex = this.form.sampleid.selectedIndex;">

However, you probably don't need this heavy a solution in this case. As you are storing the value in a hidden field, the user will not see it, and thus does not notice that the value is changing. So you can change your option tags to hold both bits of information directly...

<option value="red car---199">red car</option>

And then have your script do a split on '---' to get the two values.

This will eliminate any worries about the user not using javascript or the fact that if they accept the default option, then there was no change, so the onChange effect never happens.

Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?