dsf8897 2015-01-10 23:06
浏览 56
已采纳

在添加复选框的总和时,解析foreach()的无效参数

Since I'm basically a designer and not a developer, I've bumped into a (probably pretty simple) problem that I can not seem to figure out.

My problem:

I want to add the value of the selected checkboxes together and display the sum of them as $sum. Pretty straightforward I thought, but I seem to (1) need some form of initial check if any checkboxes are posted to avoid Warning: Invalid argument supplied for foreach(), as well as (2) some kind of prevention from posting without selecting any checkbox at all.

I've been trying to resolve my issue by looking at related threads such as Invalid argument supplied for foreach(), and I've tried to use the solutions mentioned there, but unfortunately without success – you can see the lines I've commented out, if (is_array($product)) {.

Goal

My end goal with this code is to create a form with let's say 3 segments/categories of checkboxes where the user must check at least one in each segment (they could check all if they'd like, in my sample code only the first segment/category is supplied). Each checked box will provide a value that goes towards adding them all in a sum.

I'm happy to see any alternative solutions and optimizations, as long as you can explain or point me to an example of your improvements, since I'm very much a beginner with PHP.

Index.php

/* if (is_array($product)) { */
    foreach ($_POST['product'] as $name => $value) {
        $sum = array_sum(array_map('intval', $_POST['product']));

        echo('sum: '.$sum);

    }
/* } */

?>

<form action="index.php" method="post">
    <label><input type="checkbox" name="product[1]" value="100" />100<br></label>
    <label><input type="checkbox" name="product[2]" value="200" />200<br></label>
    <label><input type="checkbox" name="product[3]" value="300" />300<br></label>

    <br>
    <input type="submit" name="submit">
</form>


<a href="index.php">reload</a>
  • 写回答

2条回答 默认 最新

  • dru5089 2015-01-10 23:14
    关注

    this should work :)

    if (isset($_POST['product'])) {
        $product = $_POST['product'] ;
         if (is_array($product)) {
            foreach ($product as $name => $value) {
                $sum = array_sum(array_map('intval', $_POST['product']));
    
                echo('sum: ' . $sum);
            }
        }
    }
    

    Or slightly optimized :)

    $product = $_POST['product'];
    if(is_array($product)) {
        $sum = array_sum(array_map('intval', $product));
        echo('sum: ' . $sum);
    }
    

    The error comes up becaus $_POST['product'] is not a valid array on the first run

    EDIT

    You is_array would work, but I dont think you declared $product before using it, if you had, my guess is your code would work :)

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

报告相同问题?

悬赏问题

  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥50 vue router 动态路由问题