简体   繁体   中英

Capturing multiple options from a drop-down list

I am trying to capture multiple options from a dropdown list and send it through mail. I have tried the following code.

HTML:

<select name="thelist[]" multiple="multiple">
<option value="Value 1">Value 1</option>
<option value="Value 2">Value 2</option>
</select>

PHP

if( is_array($thelist))
    {
        while (list ($thelist) = each ($thelist)) 
            {
                echo "$thelist <br>";
            }
    }
    else{echo "not working";}

Please Help me out.

$thelist = $_POST['thelist'];
if(is_array($thelist)){
    foreach($thelist as $item){
        echo $item.'<br>';
    }
}else{
    echo 'thelist is not an array';
}

change the thelist[] to thelist , because you are naming differently and catching with different name, do this:

<select name="thelist" multiple="multiple">
<option value="Value 1">Value 1</option>
<option value="Value 2">Value 2</option>
</select>

and as Samuel says:

$thelist = $_POST['thelist'];
if(is_array($thelist)){
  foreach($thelist as $item){
    echo $item.'<br>';
  }
}else{
  echo 'thelist is not an array';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM