【问题标题】:php inserting multiple radio values with same namephp插入多个具有相同名称的单选值
【发布时间】:2017-11-25 00:50:32
【问题描述】:

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

<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 arrays radio-button


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-13
      • 2014-11-28
      • 2015-08-27
      • 1970-01-01
      • 2017-08-03
      • 1970-01-01
      相关资源
      最近更新 更多