简体   繁体   English

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

[英]php inserting multiple radio values with same name

I'm trying to insert multiple values using radio, here is my example: 我正在尝试使用radio插入多个值,这是我的示例:

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

this one work if I insert single input, but if I want to insert more than one for example: 如果我插入单个输入,但是如果我想插入多个输入,则此方法有效:

 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

this will not work because it will merge into 1 group so if I have to choose only one of all toppings. 这将不起作用,因为它将合并为1组,因此,如果我仅选择所有浇头之一。

my original code looks like: 我的原始代码如下:

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: HTML:

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

I hope I delivered the question in the right way 我希望我以正确的方式提出了问题

please give me any idea or solution for fix this issue 请给我任何解决此问题的想法或解决方案

You should use name attribute as shown in below code. 您应该使用名称属性,如下面的代码所示。

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

This will be your $_POST after form submit 表单提交后,这将是您的$_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