0

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

2
  • make your life easy, specify the MAG array keys your self Commented May 14, 2018 at 23:41
  • $_POST['MAG'] will get all data of name="MAG[]" Commented May 15, 2018 at 1:41

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you soooo much. That really helped a lot! Still new at this so its always nice to get feedback!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.