TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   optgroup and $_POST (http://www.talkphp.com/absolute-beginners/4869-optgroup-_post.html)

niconiko 08-19-2009 09:28 AM

optgroup and $_POST
 
I cannot retrieve all the values of an optgroup set to multiple.

When I use the following PHP script, I get only one of the multiply-selected optgroup values:
PHP Code:

<?php
foreach (array_keys($_POST) as $key) {
$
$key $_POST[$key];
print 
"$key is ${$key}<br />";
}
?>

The html form script is:
html Code:
<form name="myform" action="process.php" method="POST">
<div align="center">
<select name="Vote101" multiple="multiple" size="5">
  <optgroup label="Swedish Cars">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
  </optgroup>
  <optgroup label="German Cars">
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
  </optgroup>
</select>
</div>
<br><input type="submit" value="Send"><br>
</form>

What I expect to happen is that, if someone selects "Volvo" and "Audi", then, when rerieving $_POST, I should see both "Volvo" and "Audi". As it is now, the PHP print command is only returning "Audi".

Thank you.

ModNote: read, http://www.talkphp.com/lounge/4563-p...e-talkphp.html Thank you

sketchMedia 08-19-2009 10:02 AM

change the name of the select to Vote101[], this will make PHP process it as an array.

also
PHP Code:

foreach (array_keys($_POST) as $key) {
$
$key $_POST[$key];
print 
"$key is ${$key}<br />";


can be done like so:
PHP Code:

foreach($_POST as $key => $val)
{
     echo 
$key .' is: ' $val  '<br />';


But now you will have arrays in each of the opts so:
PHP Code:

    echo 'Vote101 is: <br />';
    foreach(
$_POST['Vote101'] as $opt)
    {
        echo 
$opt '<br />';
    } 

should do it.

niconiko 08-19-2009 11:05 AM

Thank you sketchMedia.

If I can impose a bit more...

I tried combining the two foreach structures thus:

PHP Code:

<?php
foreach($_POST as $key => $val)
{
if (
$val == "Array")
  {
    foreach(
$_POST['Vote101'] as $opt)
    {
    echo 
$opt '<br />';
    }
  } else {
   echo 
$key .' is: ' $val  '<br />';
       }
}
?>

Probably obvious to you, this resulted not in what I expected but in:

Vote100 is: Milk
Vote101 is: Array

I'll try to figure this out on my own, but...


All times are GMT. The time now is 04:46 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0