Best Way to assign a single form input multiple values?

TruePaige

Diamond Member
Oct 22, 2006
9,874
2
0
Hey guys. I'm trying to figure out how to create a form that will have multiple values I can turn into variables.

<option value="Baby">Baby</option>

For example I would want you to be able to pick the "Baby" Option and have it send two values..

"Baby" and "Infant"

Just looking for an easy way to have one selection produce the two values. =)

Thanks!
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
A quick way to do it is:

<option value="baby|infant">Baby</option>

Then split on | for the value that is posted to the script.
 

Drakkon

Diamond Member
Aug 14, 2001
8,401
1
0
add the option of "multiple" to your select box and a [] to the name(will allow user to hold ctrl while selecting):

<select name="item[]" multiple>
<option value="baby">baby</option>
<option value="infant">infant</option>
<option value="other">other</option>
</select>

script should read the selected "item" as an array of the values

ex: http://www.onlinetools.org/tri...ng_multiple_select.php
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
Originally posted by: Drakkon
add the option of "multiple" to your select box and a [] to the name(will allow user to hold ctrl while selecting):

<select name="item[]" multiple>
<option value="baby">baby</option>
<option value="infant">infant</option>
<option value="other">other</option>
</select>

script should read the selected "item" as an array of the values

ex: http://www.onlinetools.org/tri...ng_multiple_select.php

From the OP: one selection, multiple values.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
You could call a javascript function with an onSubmit in the <form> tag. The javascript could read from one field and store into mutliple hidden fields.

Of course you could do that in the script code that processes the form too, and the code will probably be easier to understand.
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
Originally posted by: Drakkon
Originally posted by: tfinch2
From the OP: one selection, multiple values.
I read that as one select box multiple values :eek:

Understandable because the OP's need for this type of functionality is unclear.
 

TruePaige

Diamond Member
Oct 22, 2006
9,874
2
0
Originally posted by: tfinch2
Originally posted by: Drakkon
Originally posted by: tfinch2
From the OP: one selection, multiple values.
I read that as one select box multiple values :eek:

Understandable because the OP's need for this type of functionality is unclear.

Well the need is that I'm trying to make a simple search application, with nothing overly complex.

So you type in your search query: "Clothing" for example, and pick "Baby" and it searches two different directories.

The directories are out of my control, so I can't modify how they see the input.

So when someone picks baby I need it to, for example:

Pass Baby on as a variable: $Search

See Baby, and if Baby create a variable $Query that holds the word "Infant" instead.

I hope that helps..I was thinking it would be an if else statement but am not quite sure how I should do it.

The javascript post above was a decent suggestion, though I don't want them to have to have Javascript for basic functionality.

Thanks. =)
 

aceO07

Diamond Member
Nov 6, 2000
4,491
0
76
Seems like that's something that should be coded into the server, not the client.

If you need to pass additional values to the server, you can have hidden values with different names. Of course, it'll be based off a selection you'd need javascript to create/update that hidden value to the appropriate one each time you pick something else. So, Query would be 'baby' and the hidden Query2 would be 'infant. Query2 needs to get updated on the webpage each time you pick a different Query.

You could also use AJAX and send 2 requests to the server and display both results.

Basically, it'd be a big undertaking to attempt to do it on the client side unless it's only a few never changing items, preferably just one.