Aaron
Aaron

Reputation: 541

How to get multiple values from a single <select> variable in HTML/PHP?

Alright, so what I have is a standard select option in an HTML form, but what I'm trying to do is send over multiple values to the receiving PHP script from a single option value.

Such as something like this (I know it's incorrect):

<select name="size" id="size" type="text">
<option value="3" value="5" >3 Inches by 5 Inches</option>
<option value="6" value="4" >6 Inches by 4 Inches</option>
<option value="8" value="10" >8 Inches by 10 Inches</option>
</select>

And then on the receiving PHP script it would perhaps get some sort of "size[1], size[2]" or something. If anybody knows how to do this, any help would be terrific. I've searched around quite extensively, but I haven't seen anything quite like this. Thanks again!

Upvotes: 4

Views: 15644

Answers (2)

Lix
Lix

Reputation: 48006

What about using a separator character within your value attribute?

<option value="3_5" >3 Inches by 5 Inches</option>

Now when you come to examine those values in PHP, you can simply use explode() on the value to extract both of them.

$sizes = explode('_',$_POST['size']);

You'll now have an array containing the separated values -

array (
  0 => '3',
  1 => '5',
)  

In this example, I have chosen the underscore _ character as my separator but you could use any character you want.

Reference -

Upvotes: 1

Ibu
Ibu

Reputation: 43850

you can pass the two values in the value

<select name="size" id="size" type="text">
    ....
    <option value="6x4" >6 Inches by 4 Inches</option>
</select>

and in the backend you can split it to get the value

list($x,$y) = explode("x",$_GET['size']);  // or POST

echo $x; // 6
echo $y; // 4

Upvotes: 10

Related Questions