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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.