简体   繁体   中英

php inserting multiple radio values with same name

I'm trying to insert multiple values using radio, here is my example:

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

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:

<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

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

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

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

)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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