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 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算