Michan Posted December 6, 2006 Share Posted December 6, 2006 Hi there,I'm having some problems displaying the values of a multiple option box in PHP. I can return the data from text boxes, radio buttons, etc -- but I can't seem to find a way to display the data produced from a multiple option box.Here is my code:[code]<form method="post" action="admin_article_send.php"> <select name="game" multiple="multiple">');$selectgames = mysql_query('SELECT vg_games.id AS id, vg_games.name AS title, vg_categories.abbreviation AS cat FROM vg_games LEFT JOIN vg_categories ON vg_games.category=vg_categories.catid ORDER BY vg_games.name ASC');while ($showgames = mysql_fetch_array($selectgames)){echo (' <option value="'.$showgames['id'].'">'.$showgames['title'].' ['.$showgames['cat'].']</option>');}echo(' </select></form');[/code]This successfully selects the products listed in my database.[quote]<?PHP$game = $_POST['game'];foreach($game as $g){ echo $g; }?>[/quote]And this should display the selected options on a page.Here is my error:[quote]Warning: Invalid argument supplied for foreach() in /home/.kaybill/clubski/www.clubskill.com/csseven/admin_article_send.php on line 17[/quote]Line 17 is:[code]foreach($game as $g)[/code]It is successfully sending other values (such as the value of a text box, and a drop down category list (also plucked from the database)) - but I'm really stuck here.If anyone could help, it'd be most appreciated.-Mi Quote Link to comment https://forums.phpfreaks.com/topic/29722-posting-multiple-values-in-form/ Share on other sites More sharing options...
marcus Posted December 6, 2006 Share Posted December 6, 2006 [code]foreach ($game as $g){ echo "$g<br />";}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29722-posting-multiple-values-in-form/#findComment-136445 Share on other sites More sharing options...
Michan Posted December 6, 2006 Author Share Posted December 6, 2006 [quote author=mgallforever link=topic=117636.msg480056#msg480056 date=1165441150][code]foreach ($game as $g){ echo "$g<br />";}[/code][/quote]Thanks, but this still doesn't work. I tried something similar (using | pipes, in preparation to send the data to the database) beforehand, but just couldn't get it to work.The error is actually in the top line of the code you suggested, and I can't figure out what's causing it. Quote Link to comment https://forums.phpfreaks.com/topic/29722-posting-multiple-values-in-form/#findComment-136454 Share on other sites More sharing options...
marcus Posted December 6, 2006 Share Posted December 6, 2006 You can try:[code]<?php$game = $_POST[game];while($game = $g){echo $g;}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29722-posting-multiple-values-in-form/#findComment-136458 Share on other sites More sharing options...
kenrbnsn Posted December 6, 2006 Share Posted December 6, 2006 You need to make the name of the selection list an array:[code]<select name="game[]" multiple="multiple">[/code]Then your script will get the multiple selected values returned.Ken Quote Link to comment https://forums.phpfreaks.com/topic/29722-posting-multiple-values-in-form/#findComment-136459 Share on other sites More sharing options...
Michan Posted December 6, 2006 Author Share Posted December 6, 2006 [quote author=kenrbnsn link=topic=117636.msg480070#msg480070 date=1165441488]You need to make the name of the selection list an array:[code]<select name="game[]" multiple="multiple">[/code]Then your script will get the multiple selected values returned.Ken[/quote]Thanks Ken; I actually had the name set to an array earlier, but as I was recieving errors, I tried it without to see the result, and I guess I forgot to change it back.Even with the "game[]", I am still getting the same error message :( Quote Link to comment https://forums.phpfreaks.com/topic/29722-posting-multiple-values-in-form/#findComment-136466 Share on other sites More sharing options...
kenrbnsn Posted December 6, 2006 Share Posted December 6, 2006 Put[code]<?phpif (isset($_POST)) echo '<pre>' . print_r($_POST,true) . '</pre>';?>[/code]at the start of your script. This will dump the $_POSTed variables. The output should help you.Ken Quote Link to comment https://forums.phpfreaks.com/topic/29722-posting-multiple-values-in-form/#findComment-136471 Share on other sites More sharing options...
Michan Posted December 6, 2006 Author Share Posted December 6, 2006 [quote author=kenrbnsn link=topic=117636.msg480082#msg480082 date=1165442215]Put[code]<?phpif (isset($_POST)) echo '<pre>' . print_r($_POST,true) . '</pre>';?>[/code]at the start of your script. This will dump the $_POSTed variables. The output should help you.Ken[/quote]Thanks Ken,It looks like the values aren't actually being sent, as there's no "[game] =>" or anything similar, but all of the other results appear just fine.There's Javascript running with the form, so is it worth me posting the entire form and results page? (It's pretty hefty, mind you.)Many thanks. Quote Link to comment https://forums.phpfreaks.com/topic/29722-posting-multiple-values-in-form/#findComment-136474 Share on other sites More sharing options...
kenrbnsn Posted December 6, 2006 Share Posted December 6, 2006 Do a "show source" once you form is displayed. Post the HTML of the generated form.Ken Quote Link to comment https://forums.phpfreaks.com/topic/29722-posting-multiple-values-in-form/#findComment-136477 Share on other sites More sharing options...
Michan Posted December 6, 2006 Author Share Posted December 6, 2006 [quote author=kenrbnsn link=topic=117636.msg480088#msg480088 date=1165442768]Do a "show source" once you form is displayed. Post the HTML of the generated form.Ken[/quote]Thanks Ken; the code is too long for the forum post, so I've uploaded it here: http://www.stabhead.com/code.html Quote Link to comment https://forums.phpfreaks.com/topic/29722-posting-multiple-values-in-form/#findComment-136499 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.