0

```

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <form action="process.php" method="post">
    <input type="checkbox" name="top[]" value="pep">Pepperoni<br>
    <input type="checkbox" name="top[]" value="msh">Mushrooms<br>
    <input type="checkbox" name="top[]" value="olv">Olives<br>
    <input type="submit" name="submit">
  </form>

</body>

</html>
``` ```
<?php
if (isset($_POST['submit'])) {
    $toppings = filter_input (INPUT_POST, 'top', FILTER_SANITIZE_SPECIAL_CHARS, FILTER_REQUIRE_ARRAY);
    if (toppings !==null) {
        foreach (toppings as  $key => $value) {
            echo $key .'=' .$value . '<br>';
        }
    } else {
        echo 'No toppings selected.';
    }
}

?>

results on my local host

Warning: Use of undefined constant toppings - assumed 'toppings' (this will throw an Error in a future version of PHP) in C:\xampp\htdocs\Toppings_Display\process.php on line 4

Warning: Use of undefined constant toppings - assumed 'toppings' (this will throw an Error in a future version of PHP) in C:\xampp\htdocs\Toppings_Display\process.php on line 5

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\Toppings_Display\process.php on line 5

Akshay Mulgavkar
  • 1,727
  • 9
  • 22
gto
  • 1
  • You should declare variable with `$`, like `$toppings`. Please get some tutorials, or read [official document](https://www.php.net/manual/en/language.variables.basics.php) first. – Calos Jan 21 '20 at 02:35
  • `toppings` should be `$toppings` in this code: `if (toppings !==null) { foreach (toppings as $key => $value) {` – Nick Jan 21 '20 at 02:35
  • Your issue is the difference between a variable and a constant. The code thinks the line toppings without the dollar sign is a constant as you are referencing it in your for loop. All variables in PHP start with a $ (dollar) sign followed by the name of the variable; $toppings, where toppings has no dollar sign in your for loop. You must indicate to the code that you are evaluating a variable and not a constant. Constant could be the following: define('toppings', array(''pep', 'olv', 'msh')); Obviously you have not declared a constant and php thinks it is a constant therefor the error. – dale landry Jan 21 '20 at 05:48
  • Many thanks for the correction. – gto Jan 23 '20 at 05:07

0 Answers0