dongxie45083 2018-05-14 23:34
浏览 77
已采纳

PHP - 如何获取多个复选框的值以及输入类型

I wanted to create an error when a specific value of $_POST['MAG'] is selected and the input type next to it isn't given.

I've tried to use other methods like if, else, foreach, for, and I still can't seem to get the right coding. I need really need help.

Its my first time coding and i'm taking online classes so its a bit difficult to get help from classmates or teacher

 <form method="post" action="">

  Select which magazine and type qty of subscriptions:<br><br>

  <input type="checkbox" name="MAG[]" value="TREASURE"><b>Treasure Magazine</b> | Qty of Subscriptions: <input type="number" name="TNUMSUBS" size="5px">
  <br>

  <input type="checkbox" name="MAG[]" value="VESSEL"><b>Vessel Magazine</b> | Qty of Subscriptions: <input type="number" name="VNUMSUBS" size="5px">
  <br>

  <input type="checkbox" name="MAG[]" value="MECH"><b>MECH Magazine</b> | Qty of Subscriptions: <input type="number" name="MNUMSUBS" size="5px">
  <br><br>

  <p><input type="submit" name="submit"></p>

</form>


<?php
if (isset($_POST["submit"]))
{
$vNumSubs = $_POST['VNUMSUBS'];
$tNumSubs = $_POST['TNUMSUBS'];
$mNumSubs = $_POST['MNUMSUBS'];

    if(empty($_POST["MAG"]))
    {
        print "You didn't select a magazine";

        foreach($_POST['MAG'] as $magazine)
        {

            if($magazine == "TREASURE" && empty($tNumSubs))
            {
            print "type quantity";
            }

            if($magazine == "VESSEL" && empty($vNumSubs))
            {
                print "type quantity";
            }

            if($magazine == "MECH" && empty($mNumSubs))
            {
                print "type quantity";
            }
        }
    }


}

I tried using the empty() and i'm having a hard time makign it show. please help

  • 写回答

1条回答 默认 最新

  • dsfsfsfsfs55656 2018-05-14 23:49
    关注

    Your foreach is in the block that's executed when $_POST['MAG'] is empty, so there's nothing to loop over (it will actually get an error, since $_POST['MAG'] is undefined when none of the boxes are checked, and you can't use foreach on this).

    It should be in the else block.

    if(empty($_POST["MAG"])) {
        print "You didn't select a magazine";
    } else {
        foreach($_POST['MAG'] as $magazine)
        {
            if($magazine == "TREASURE" && empty($tNumSubs))
            {
                print "type quantity";
            }
    
            if($magazine == "VESSEL" && empty($vNumSubs))
            {
                print "type quantity";
            }
    
            if($magazine == "MECH" && empty($mNumSubs))
            {
                print "type quantity";
            }
        }
    }
    

    But maybe you shouldn't bother with the checkboxes. Just let them fill in the quantity of each magazine, and use 0 to mean that they don't want that magazine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看