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条)

报告相同问题?

悬赏问题

  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。