SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    May 2013
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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;
    PHP Code:
      <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;
    PHP Code:
    $movie= array($_POST['movie']); 
    and to display it;

    PHP Code:
    <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!

  2. #2
    SitePoint Member
    Join Date
    May 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you made mistake at

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

    try using

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

    This will join the elements with , (comma)

    Thanks

  3. #3
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,014
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    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?
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  4. #4
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    This is wrong:
    PHP Code:
    $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:
    PHP Code:
    $movie $_POST['movie']; 
    $movie is now an array. To list the titles you could use a foreach loop
    PHP Code:
    $movie $_POST['movie'];
    foreach(
    $movie AS $title) {
        echo 
    "{$title}<br>\n";

    And, yes, it's a good idea to sanitize the input.
    Denny Schlesinger
    web services

  5. #5
    SitePoint Member
    Join Date
    May 2013
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •