繁体   English   中英

php插入多个具有相同名称的单选值

[英]php inserting multiple radio values with same name

我正在尝试使用radio插入多个值,这是我的示例:

<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";}?>>

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

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

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

<?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
        )

)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM