SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post results from a Multiple Select

    I tried searching the forum - but unfortunately I kept getting server errors.

    Here is my HTML:
    Code:
    <select name="drpYear[]" id="drpYearID" multiple="multiple" size="5">
    <option value="2011" > 2011
    <option value="2010" > 2010
    <option value="2009" > 2009
    <option value="2008" > 2008
    <option value="2007" > 2007
    </select>
    this all appears fine on my page

    However, when this gets POSTed - i'm only able to see the topmost selected item.

    Code:
    $cnt = count( $_POST[ "drpYear" ] );
    $yrlist = $_POST[ "drpYear" ];
    $yr = implode( ', ', $yrlist );
    No matter how many I select, $cnt = 1 & $yr comes back with 2011 ( or whichever the highest one selected is ).

    Now, everything I read said adding [] to the select name, and making the ID differ would work - but its not working for me.

    Any ideas?

  2. #2
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,200
    Mentioned
    105 Post(s)
    Tagged
    1 Thread(s)
    That doesn't look right (but I've been bouncing between php and c# all day, so take that with a grain of salt....)

    Try this:
    Code PHP:
    <?php
    $yearList = $_POST['drpYear'];
    if ($yearList) {
       foreach ($yr as $yearList) {
          echo "<p>{$yr}</p>"; 
       }
    }
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    You got your foreach backwards, Dave. foreach($yearList as $yr)

  4. #4
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks -

    However - the implode is right and works fine ( I am using it elsewhere on the page in a similar way ( using multiple check boxes for the months ) ), and I've used it before on other items.

    The issue is that $cnt = 1 - and it should equal 3 ( i'm testing with 3 years selected ). And $yr should equal 2011, 2009, 2007 as those are the ones i've selected. But my post[ 'drpYear' ] is not bringing those over.

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Standard issue Response:
    PHP Code:
    var_dump($_POST['drpYear']); 
    Result is...?

  6. #6
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Result is:

    array(1) {
    [0]=>
    string(4) "2011"
    }

    Again - select 2011, 2009, 2007

  7. #7
    SitePoint Addict beebs93's Avatar
    Join Date
    Jul 2010
    Location
    Vancouver, Canada
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Forgive the dumb question, but how come in your example you didn't close the option tags?
    "To be truly dedicated to something
    you must be willing to betray it.
    " -SW

  8. #8
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Probably because all of the examples I was using for Multiple Selects were older and didn't have the </option> on it - so I didn't think about it.

  9. #9
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    and that'd probably solve your problem...the problem doesnt seem to be in PHP, it's in your HTML. Only 1 item is being passed on.

  10. #10
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well - I added that on - and it didn't fix anything...

    Here is the results from firebug in Mozilla for that section of the code:

    Code:
    <select name="drpYear[]" id="drpYearID" multiple="multiple" size="5"><option value="2011" >2011</option><option value="2009" >2009</option><option value="2008" >2008</option><option value="2007" >2007</option><option value="2010" >2010</option></select>
    and here is my var_dump again:
    Code:
    array(1) {
      [0]=>
      string(4) "2011"
    }
    So - no difference with the </option>

  11. #11
    SitePoint Addict beebs93's Avatar
    Join Date
    Jul 2010
    Location
    Vancouver, Canada
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code HTML4Strict:
    <form action="" method="POST">
    <select name="drpYear[]" id="drpYearID" multiple="multiple" size="5">
    <option value="2011" > 2011</option>
    <option value="2010" > 2010</option>
    <option value="2009" > 2009</option>
    <option value="2008" > 2008</option>
    <option value="2007" > 2007</option>
    </select>
    <input type="submit" name="submit" value="POST" />
    </form>

    I just ran this and ran print_r($_POST) and got all options I selected. Can you trying running this and tell us what you get?
    "To be truly dedicated to something
    you must be willing to betray it.
    " -SW


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
  •