4

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!

3
  • <select name="size" id="size" type="text" multiple="multiple"> Commented Oct 8, 2012 at 23:52
  • Why not just use value="3x5", value="6x4", etc.? Commented Oct 8, 2012 at 23:52
  • perhaps checkboxes would be better? Commented Oct 8, 2012 at 23:52

2 Answers 2

10

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
Sign up to request clarification or add additional context in comments.

4 Comments

+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.
@NullUserException you are correct, if javascript is used this is a good solution too
Terrific, thank you for the tips. Anyhow, this is exactly what I was looking for! Thank you very much Ibu!
@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.
1

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 -

2 Comments

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.
@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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.