php插入多个具有相同名称的无线电值

时间:2017-11-24 09:31:08

标签: php arrays radio-button

我试图使用无线电插入多个值,这是我的例子:

<input type="radio" name="toppingPrice[]" value="<?= $topping['name'];?>-<?= $topping['price'];?>">

如果我插入单个输入,这个工作,但如果我想插入多个例如:

 Size: small, medium, large <- name="toppingPrice[]" for all input values
 Cheese: yes, no <- name="toppingPrice[]" for all input values
 Level: spicy, normal <- name="toppingPrice[]" for all input values

这不起作用,因为它将合并为1组,所以如果我只需要选择所有浇头中的一个。

我的原始代码如下:

foreach ($toppingPrice as $key) {
        list($toppingName,$toppingNameEn, $toppingPrice) = explode("-",$key,3);
        $tName[] =  $toppingName;
        $tNameEn[] =  $toppingNameEn;
        $tPrice += $toppingPrice;
    }
$tn = implode(",", $tName);
    $tn_en = implode(",", $tNameEn);
    $price = $price + $tPrice;

HTML:

<input type="checkbox" name="toppingPrice[]" id="<?= $topping[0];?>" value="<?= $topping['name'];?>-<?= $topping['name_en'];?>-<?= $topping['price'];?>" <? if($topping['price'] < 1){echo "checked";}?>>

我希望我以正确的方式提出问题

请给我任何解决此问题的想法或解决方案

1 个答案:

答案 0 :(得分:0)

您应该使用name属性,如下面的代码所示。

<?php
echo "<pre>";
print_r($_POST);
echo "</pre>";
?>

<form name="test" method="post">
<input type="radio" name="toppingPrice.size[]" value="1"> 1
<input type="radio" name="toppingPrice.size[]" value="2"> 2
<input type="radio" name="toppingPrice.size[]" value="3"> 3

<br>

<input type="radio" name="toppingPrice.toping[]" value="4"> 4
<input type="radio" name="toppingPrice.toping[]" value="5"> 5
<input type="radio" name="toppingPrice.toping[]" value="6"> 6

<br>

<input type="radio" name="toppingPrice.level[]" value="7"> 7 
<input type="radio" name="toppingPrice.level[]" value="8"> 8
<input type="radio" name="toppingPrice.level[]" value="9"> 9

<button type="submit" value="save" /> Save
</form>

这将是您提交表单后的$_POST

Array
(
    [toppingPrice_size] => Array
        (
            [0] => 1
        )

    [toppingPrice_toping] => Array
        (
            [0] => 5
        )

    [toppingPrice_level] => Array
        (
            [0] => 9
        )

)