AL123 Posted December 11, 2009 Share Posted December 11, 2009 I have been working from O'Rileys php5 book, looping through select menus. That works fine with my single select menus: I post, the values change. But my multiple select menu just prints to the screen and post does nothing. Here is my simple site: http://nuke.empireindustry.com/Blogspiracy/Test/index.php This is giving me a headache - probably something simple I just don't get. AL123. <?php // Controller for test Page. include '../config.php'; $pizza = array('1' => 'Pepperoni', '2' => 'Canadian Bacon', '3' => 'Meat Medley'); $pSize = array('1'=>'Small', '2'=>'Medium', '3'=>'Large'); $tMulti = array('1'=>'Extra chese', '2'=>'Canadian Bacon', '3'=>'Sausage', '4'=>'olives'); function show_form() { // start form echo "<form method='post' action='$_SERVER[php_SELF]' > "; // pizza select menu echo "Select a Pizza:<select name='pizza'> "; foreach($GLOBALS['pizza'] as $val => $choice) { print "<option value=\"$val\">$choice</option> \n"; } echo "</select><br /><br /> "; // size select menu echo "Size:<select name='pSize'> "; foreach($GLOBALS['pSize'] as $val => $choice) { print "<option value=\"$val\">$choice</option> \n"; } echo "</select><br /><br /> "; // extra topping menu echo "Extra Toppings:<select name='topMulti[]' multiple='multiple'> "; foreach($GLOBALS['tMulti'] as $val => $choice) { print "<option value=\"$val\">$choice</option> \n"; } echo "</select><br /><br /> "; //end form echo "Order:<input type='submit' NAME='button' value='Verify'> <input type='hidden' name='submit_check' value='1'> </form> "; } function process_form() { echo "<br />"; echo "Pizza: " . $_POST['pizza']; echo "<br />"; echo "Size: " . $_POST['pSize']; echo "<br />"; if($_POST){ foreach($GLOBALS['tMulti'] as $choice) { echo "you chose $choice <br />"; } } show_form(); } if($_POST['submit_check']) { process_form(); } else { show_form(); } Quote Link to comment Share on other sites More sharing options...
RussellReal Posted December 11, 2009 Share Posted December 11, 2009 foreach($GLOBALS['tMulti'] as $choice) you're using your variable $tMulti which is just your array.. you're gonna want to loop through $_POST['topMulti'][0] like this: <?php foreach ($_POST['topMulti'][0] as $v) { echo "You chose: {$v}\n<br />\n"; } ?> Quote Link to comment Share on other sites More sharing options...
AL123 Posted December 11, 2009 Author Share Posted December 11, 2009 I changed the loop as you suggested and this error happens: Warning: Invalid argument supplied for foreach() in /home/nuke/public_html/Blogspiracy/Test/ctrl.php on line 51 Quote Link to comment Share on other sites More sharing options...
AL123 Posted December 11, 2009 Author Share Posted December 11, 2009 I almost fixed it with this: if(array_key_exists){ foreach($_POST['topMulti'] as $val) { echo "you chose: $val <br />"; } } now I just cant seem to correct this error: Warning: Invalid argument supplied for foreach() in /home/nuke/public_html/Blogspiracy/Test/ctrl.php on line 51 any suggestions on how to "do nothing" if no item is selected? AL123 Quote Link to comment Share on other sites More sharing options...
RussellReal Posted December 11, 2009 Share Posted December 11, 2009 honestly I don't see how that loop works because the select's name is: <select multiple="multiple" name="topMulti[]"> an array lol but if it does work.. than good on you.. just do something like this if (count($_POST['topMulti'])) { // put the loop in here } Quote Link to comment Share on other sites More sharing options...
AL123 Posted December 11, 2009 Author Share Posted December 11, 2009 That worked great! Thanks a lot, I was not aware of count(). AL123. This is how it is now: if(count($_POST['topMulti'])) { foreach($_POST['topMulti'] as $val) { echo "you chose: $val <br />"; } } Quote Link to comment Share on other sites More sharing options...
RussellReal Posted December 11, 2009 Share Posted December 11, 2009 Anytime Phpfreaks.com is full of information and great helpers, which can more than likely help you out! But if you're ever interested in getting help from me or just chatting, my MSN is in my signature Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.