Getting multiple select values

I’m new to php and I’m having a bit of problem with figuring this out.

I’m just wondering what the method is to get several select values to display

This is my html;

  <p>Your favourite movie: <select name="movie[]" size="4" multiple="multiple">
                             <option value="Batman">Batman</option>
                             <option value="Ironman">Ironman</option>
                             <option value="Twilight">Twilight</option>
                             ..............

And in my php file I have this;

$movie= array($_POST['movie']);

and to display it;

<strong>Favourite movie = </strong> <?php echo "$movie"; ?>

It’s all really basic as I’ve just started learning it. I did google it and try different ways but not sure why it still isn’t working.

Any help on this will be high appreciated. Thanks! :slight_smile:

I think you made mistake at

$movie= array($_POST[‘movie’]);

try using

$movie= implode(', ',$_POST[‘movie’]);

This will join the elements with , (comma)

Thanks

Any user submitted data (such as the contents of the $_POST array) needs to be sanitized to make sure that you’re not opening your site up to attack.

Will the user have a fixed number of options to choose from or will it vary?

This is wrong:

$movie= array($_POST['movie']);

Since you have square brackets after “movie” the multiple selection is already in an array as part of the $_POST array. To extract the movie array you just need:

$movie = $_POST['movie'];

$movie is now an array. To list the titles you could use a foreach loop

$movie = $_POST['movie'];
foreach($movie AS $title) {
    echo "{$title}<br>\
";
}

And, yes, it’s a good idea to sanitize the input.

Thanks guys for the help! Makes a lot more sense now, really do appreciated it.

Haven’t encountered sanitized inputs before but I will look into it.