簡體   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