douzuita7325 2014-03-25 01:14
浏览 60
已采纳

通过php中的复选框表单选择数组中的项目

I tried to create a form from an array and make the items selectable and be able to collect items individually for processing as variables but i get blank screen when i do echo $_POST['citem1']

<?php
foreach($_SESSION['cart'] as $data1 => $data2)
{echo
'<input type = "checkbox" name = "citem1" value = "'.$data1.'"/> ' .  
 ' price:', $data2 . "<br>";}

?>

What I really want to do is to be able to select one or more of the items and put them in separate variables. Help please.

var_dump of $_SESSION=array (size=3)
'sugar' => string '989.32' (length=6)
'lolli' => string 'inquire' (length=7)
'caramel unreal' => string '00.00' (length=5)
  • 写回答

3条回答 默认 最新

  • duanluwei9374 2014-03-25 01:26
    关注

    The name of field is 'citem1', so you have to get this value with $_POST['citem1'].

    To send array you have to name like 'citem1[]', and items will receive by PHP like array:

    <?php
    foreach($_SESSION['cart'] as $data1 => $data2)
    {
    echo
    '<input type = "checkbox" name = "citem1[]" value = "'.$data1.'"/> ' .  
     ' price:', $data2 . "<br>";}
    
    ?>
    

    Or, to send key=>value like this:

    <?php
    foreach($_SESSION['cart'] as $data1 => $data2)
    {
    echo '<input type = "checkbox" name = "'.$data1.'" value = "'.$data2.'"/> ' .  
    ' price:', $data2 . "<br>";
    }
    
    ?>
    

    But in this case you need to loop through $_POST, so a better way is:

    <?php
    foreach($_SESSION['cart'] as $data1 => $data2)
    {
    echo '<input type = "checkbox" name = "Item['.$data1.']" value = "'.$data2.'"/> ' .  
    ' price:', $data2 . "<br>";
    }
    
    ?>
    

    With name="Item[field]" you can loop through $_POST['Item'] and get key=>value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?