1

An assignment question reads as follow

enter image description here

I have created the function, as follows:

function getToppings($toppings){
    if($toppings=='') {
        echo 'You have to select at least one topping';
       die();
    }//if
        foreach($toppings as $index => $selected) {

            $toppings[$index] = $selected;
            echo $selected;
            echo '<br />';
        }//foreach
}//function

HTML

Select Your Topping

<form name="select-topping" method="post">
    <label for="toppings">Peperoni</label>
    <input type="checkbox" name="toppings[]" value="peperoni">
    <br />
    <label for="toppings">Salami</label>
    <input type="checkbox" name="toppings[]" value="salami">
    <br />
    <label for="toppings">Ham</label>
    <input type="checkbox" name="toppings[]" value="ham">
    <br >
    <label for="toppings">Mushroom</label>
    <input type="checkbox" name="toppings[]" value="mushroom">
    <button type="submit" name="submit" class="btn btn danger">Submit</button>
</form>

Function Call

if(isset($_POST['submit'])){
    getToppings($_POST['toppings']);
}

The functions works perfectly, and prints all toppings, selected, however my problem comes in when no toppings are selected (validation)

MY PROBLEM

When no topping is selected I get the following, error:

enter image description here

So eventhough my form validates, I still get the error undefined variable:

Now I am pretty sure I know why I get the error message, it is because if no toppings were selected, no parameters gets passed to function...correct? What I do not know is how to fix / improve on it...any advice or help appreciated.

2
  • Do you want to add a javascript for this or just plain PHP for validation? Because if i'm going to look at your getToppings function, $toppings is an array, you can first count the array like this count($toppings) == 0 Commented Oct 20, 2016 at 2:33
  • @L.Herrera I have to use only PHP for this assignment unfortunately Commented Oct 20, 2016 at 2:36

1 Answer 1

2

In your function call, you are not checking if $_POST['toppings'] is set, before you pass it to getToppings(), modify you function call to this:

if(isset($_POST['submit']) && isset($_POST['toppings'])){
    getToppings($_POST['toppings']);
} elseif(isset($_POST['submit']) && !isset($_POST['toppings'])){
    getToppings('');
}

EDIT

This is another way to do it, since the index you are selecting is not being set, you have to check it before you call it, there is no other way. So you can do this:

if(isset($_POST['submit'])){
    getToppings((isset($_POST['toppings'])) ? $_POST['toppings'] : '');
}
Sign up to request clarification or add additional context in comments.

2 Comments

HI thank you for your answer Neal, yeah I know I can do that -- what I would like to know is if there is a way in which I can validate toppings WITHIN THE FUNCTION, without having to make 2 functions calls in an if else statment
@Marilee how about this approach?

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.