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

Ok, so I have a standard selection option in the HTML form, but I'm trying to send multiple values ​​to the receiving PHP script from the same parameter value .

For example, something like this (I know this is wrong):

<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 will probably get some sort of β€œsize [1], size [2]” or something else. If anyone knows how to do this, any help would be awesome. I searched quite widely, but I did not see anything like it. Thanks again!

+4
source share
2 answers

you can pass 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 
+9
source

How about using a separator character in your value attribute?

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

Now that you come to learn these values ​​in PHP, you can simply use explode() for the value to extract both of them.

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

You will now have an array containing the divided values ​​-

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

In this example, I selected the underscore character _ as my separator, but you can use any character you want.

Link -

+1
source

Source: https://habr.com/ru/post/1438524/


All Articles