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

15,310

Solution 1

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

Solution 2

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 -

Share:
15,310
Aaron
Author by

Aaron

Updated on July 06, 2022

Comments

  • Aaron
    Aaron almost 2 years

    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!

    • Tieson T.
      Tieson T. over 11 years
      <select name="size" id="size" type="text" multiple="multiple">
    • NullUserException
      NullUserException over 11 years
      Why not just use value="3x5", value="6x4", etc.?
    • AlexP
      AlexP over 11 years
      perhaps checkboxes would be better?
  • NullUserException
    NullUserException over 11 years
    +1 This is the sane way. Alternatively you could use an array field name (eg: sizes[]) and use JS to assign multiple values to it upon selecting one of them. But this probably isn't worth the hassle.
  • Ibu
    Ibu over 11 years
    @NullUserException you are correct, if javascript is used this is a good solution too
  • Aaron
    Aaron over 11 years
    Terrific, thank you for the tips. Anyhow, this is exactly what I was looking for! Thank you very much Ibu!
  • Aaron
    Aaron over 11 years
    Thank you very much, this is exactly what I was looking for! I'll be marking the answer from Ibu as correct only because he has the output list with variables which is how I would prefer it. Thank you very much for the time.
  • Lix
    Lix over 11 years
    @aar - no worries :) Happy to help! In my experience, using the list command leaves you more prone to mistakes but its your project and not mine in this case :)
  • Ibu
    Ibu over 11 years
    @Aaron im glad i could help, one suggestion though would be to make sure to check the values of parameters in the POST or GET before using them.