Jump to content

Multiple Select and foreach loop


AL123

Recommended Posts

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();
}

 

Link to comment
https://forums.phpfreaks.com/topic/184737-multiple-select-and-foreach-loop/
Share on other sites

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";
  }
?>

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

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

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.