Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

php menu in loop

Posted on 2011-02-23
10
Medium Priority
?
389 Views
Last Modified: 2012-06-27
hello,

I am having a problem with a dropdown menu in php, the menu works fine, it displays everything as it should, but it will not pass down any values. the reason there is a $i next to it is because the number of menus must be the same as the counter in the loop, and each menu must have its own name obviously, so I opted for 'firstname$i' where the first menu would be seen as firstname0, 2nd being firstname1 etc....  the [] are there because tjhe user must be able to select multiple options from the menu.

the code section is:

<?

?>

<FORM method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?


$i = 0;
while ( $i <= 5 ) {

//echo"Firstname: <input type='text' name='firstname$i'[] />";


echo"<select name= 'firstname[$i]'[] size=4 multiple>
  <option>Milk</option>
  <option>Coffee</option>
  <option>Tea</option>
</select>";

$i++;

}

?>

</Select>
<input type=Submit value=pass on>
</form>

<?


$var2 = $_POST['firstname0'];



print_r($var2);
?>

please can someone show me where it is that I am going wrong.

thanks, Gavin
0
Comment
Question by:GPB1983
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 2000 total points
ID: 34959916
In this line

echo"<select name= 'firstname[$i]'[] size=4 multiple>

Move the quote between the ] [

echo"<select name= 'firstname[$i][]' size=4 multiple>
0
 

Author Comment

by:GPB1983
ID: 34960014
hiya,

thanks for the quick reply, but I have already tried that on another example, the code compiles fine, but doesnt work. it doesnt pass anything.

the url for it is:
http://www.gavinbuczko.co.uk/phplooptrial.php

regards, Gavin
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34960133
Works for me.....

_POST["firstname"]      

Array
(
    [0] => Array
        (
            [0] => milk
            [1] => coffee
            [2] => tea
        )

    [2] => Array
        (
            [0] => milk
            [1] => coffee
        )

    [4] => Array
        (
            [0] => milk
            [1] => tea
        )

    [5] => Array
        (
            [0] => coffee
            [1] => tea
        )

)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:GPB1983
ID: 34960208
hi thanks, yes it does, but what I need it to do is if i choose stuff from menu one then it prints that, if i select stuff from menu 2 then print that, at the moment it prints everything out.

regards, Gavin
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34960248
No - it only prints what is selected. If you look at the above you will see that the array indexes are 0, 2, 3, 4 and 5. Index number 1 is missing because I did not select anything on that menu.

Perhaps I'm missing the point?
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34960251
Actually index 3 is missing as well.
0
 

Author Comment

by:GPB1983
ID: 34960295
Hi, sorry I mis understood, but you are right.
what I was trying to do was to get the contents of menu 0 into

$var2 = $_POST['firstname0'];
$var3 = $_POST['firstname1'];

etc..

sorry I should have explained better.

regards, Gavin
0
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 2000 total points
ID: 34960420
Something like this will pick out the data

<?php
if (isset( $_POST['firstname'] ) ) {
     $data = $_POST['firstname'];
     

     foreach( $data as $index => $aGroup ) {
          echo "Group number $index selected ";

          foreach( $aGroup as $aValue )
               echo "$aValue ";

          echo "<br/>";

     }
}

Open in new window


It could be modified to store the data easily enough
0
 

Author Comment

by:GPB1983
ID: 34961739
thanks mate, I really appreciate all the help, and the detailed explanation! top marks allround mate! thanks,
regards,
Gavin
0
 

Author Closing Comment

by:GPB1983
ID: 34961749
top quality explanations!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question