Posted by Jerry Stuckle on 12/17/17 11:56
Lenard Redwood wrote:
> I have the maximum number of warnings during PHP development. How can I
> get rid of these warnings without turning them off in PHP?
>
> Here's the code I have:
>
> <form method="POST" action="eat.php">
> <select name="lunch[ ]" multiple>
> <option value="pork">BBQ Pork Bun</option>
> <option value="chicken">Chicken Bun</option>
> <option value="lotus">Lotus Seed Bun</option>
> <option value="bean">Bean Paste Bun</option>
> <option value="nest">Bird-Nest Bun</option>
> </select>
> <input type="submit" name="submit">
> </form>
>
> Selected buns: <br/>
> <?php
> foreach ($_POST['lunch'] as $choice) {
> print "You want a $choice bun. <br/>";
> }
> ?>
>
>
> And I get: Warning: Invalid argument supplied for foreach() in
> C:\lighttpd\htdocs\index.php on line 17
>
> Is there a way to give the 'lunch' variable a default value if
> undefined?
> Thank you.
>
Make sure $_POST['lunch'] is set and is an array. Check out isset() and
is_array().
And ALWAYS validate information coming from the user. NEVER assume
you're getting something.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
[Back to original message]
|