如何在HTML/PHP中从单个变量中获取多个值?

时间:2022-10-29 13:10:51

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.

好的,我有一个HTML表单中的标准选择选项,但是我要做的是将多个值从一个选项值发送到接收的PHP脚本。

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!

然后在接收PHP脚本上,它可能会得到某种“大小[1],大小[2]”之类的东西。如果有人知道怎么做,任何帮助都将是非常棒的。我到处找过了,但还没见过这样的东西。再次感谢!

2 个解决方案

#1


9  

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

#2


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.

现在,当您在PHP中检查这些值时,您只需在值上使用explosion()来提取这两个值。

$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 -

参考,

#1


9  

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

#2


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.

现在,当您在PHP中检查这些值时,您只需在值上使用explosion()来提取这两个值。

$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 -

参考,