SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2001
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry Multiple Select Options Within a Form

    Hi - I have a form with multiple select options:

    Code:
      <select name="optionlist" size="8" class="Form" id="optionlist" cols="35" rows="8" multiple>
    <option>Option 1 </option> 
    <option>Option 2 </option>	 
    <option>Option 3 </option> 
    </select>
    Now, the php for this select looks like this:

    PHP Code:
     $msg .= "Option List Results: {$_POST['optionlist']}\n"
    My problem is that when I test it by selecting all three options the final php only publishes the last option. What do I need to add to my html or php?
    ---no sig here---

  2. #2
    SitePoint Enthusiast kachi_face's Avatar
    Join Date
    Feb 2005
    Location
    Bauchi, Nigeria
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you must catch the results as an array, the same way you catch values from checkboxes
    The Dot.NET Killer! - 3nity-face Media

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2001
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you give me some example code - my php knowledge is close to 0... thanks.
    ---no sig here---

  4. #4
    SitePoint Addict
    Join Date
    May 2003
    Location
    Auckland
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HTML:

    The square bracket in the <select /> name will make php turn it into an array when the form is posted.
    Code:
       <select name="optionlist[]" multiple="multiple" size="10">
       	<option value="1">1</option>
       	<option value="2">2</option>
       	<option value="3">3</option>
       </select>
    php:
    Code:
      $selectedOptions = array();
      $tempOptions = $_POST['optionlist'];
      if (is_array($tempOptions)) {
      	$selectedOptions = $tempOptions;
      }
      
      echo 'User has selected ' . count($selectedOptions ) . ' options<br />';
      foreach ($selectedOptions as $optionValue) {
      	echo 'Value: ' . $optionValue . '<br />'
      }

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2001
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Imminent. Would your php work for an email confirmation page? I need php to publish the final selected values in an email message. What would I need to change in my current
    Code:
     $msg .= "Option List Results: {$_POST['optionlist']}\n"; 
    to to handle the array?

    FYI, my email form looks like this:

    PHP Code:
    <?
     
     $msg 
    "Info Request - {$_SERVER['PHP_SELF']}\n";
     
    $msg .= "Name: {$_POST['name']}\n"
     
    $msg .= "Email: {$_POST['email']}\n"
     
    $msg .=  "Option List Results:  {$_POST['optionlist']}\n";
     
     ...
    ---no sig here---

  6. #6
    Non-Member
    Join Date
    Jul 2005
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    he just posted the code you need to use

  7. #7
    SitePoint Addict
    Join Date
    May 2003
    Location
    Auckland
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are using my code above then do this:
    Code:
     $msg = '';
     $msg .= "Info Request - {$_SERVER['PHP_SELF']}\n";
     $msg .= "Name: {$_POST['name']}\n";
     $msg .= "Email: {$_POST['email']}\n";
     $msg .=  "Option List Results:\n";
     foreach ($selectedOptions as $value) {
     	$msg .= $value . "\n";
     }

  8. #8
    SitePoint Zealot
    Join Date
    Mar 2001
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Imminent - just tried this and noticed there was no reference to the form name 'optionlist' - where would I incorporate that in your example? Thanks!
    ---no sig here---

  9. #9
    SitePoint Zealot
    Join Date
    Mar 2001
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone have any insight into this?
    ---no sig here---

  10. #10
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Bloomington, IL
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Replace $selectedOptions with $_POST['optionlist'] in Imminent's example.

  11. #11
    SitePoint Zealot
    Join Date
    Mar 2001
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Bitmesh - the field still came back blank for me after testing...
    ---no sig here---

  12. #12
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Bloomington, IL
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's the HTML for the select box:
    Code:
    <select name="optionlist[]" multiple="multiple" size="10">
       	<option value="1">1</option>
       	<option value="2">2</option>
       	<option value="3">3</option>
    </select>
    And here is the PHP:
    PHP Code:
    $msg '';
    $msg .= "Info Request - {$_SERVER['PHP_SELF']}\n";
    $msg .= "Name: {$_POST['name']}\n";
    $msg .= "Email: {$_POST['email']}\n";
    $msg .=  "Option List Results:\n";
    foreach (
    $_POST['optionlist'] as $value) {
       
    $msg .= $value "\n";

    That should work.

  13. #13
    SitePoint Zealot
    Join Date
    Mar 2001
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks so much Bitmesh - you saved me alot of hair!
    ---no sig here---


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
  •