#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    393
    Rep Power
    47

    Trouble with JQuery multiple select


    I'm having trouble with a JQuery multiple select.

    I have the following code which allows the user to select items from one list (select1) by adding them to another list (select2):

    PHP Code:
     <div>  
      <
    select multiple id="select1" name="select1">  
       <
    option value="1">Option 1</option>  
       <
    option value="2">Option 2</option>  
       <
    option value="3">Option 3</option>  
       <
    option value="4">Option 4</option>  
      </
    select>  
      <
    a href="#" id="add">add &gt;&gt;</a>  
     </
    div>  
     <
    div>  
      <
    select multiple id="select2" name="select2"></select>  
      <
    a href="#" id="remove">&lt;&ltremove</a>  
     </
    div>  
    <
    INPUT TYPE='submit' NAME='lesson_submit' VALUE='Save Changes'
    The selection method works great and values in "select1" move to "select2" when the "add" link is clicked.

    In theory, the values added to "select2" should be available in an array, so I am trying to print them to the browser as such:

    PHP Code:
    if(isset($_POST['lesson_submit'])){
    echo 
    "begin output:<br>";
    $array $_POST['select1'] ;
    foreach(
    $array AS $s)
    {
    "$value = $s " ;
    echo 
    "Value: $value<br>";
    }
    echo 
    "end output:<br>"
    However, the only thing that is printed is the first "begin output" statement.

    Can anyone see what I am doing wrong?
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    you're trying to print the first one rather than the second, for one thing.

    print_r($_POST) and see what's in there.

    There's no code anywhere in here that actually handles moving the items between the lists. Perhaps that code is wrong, wherever it is.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,303
    Rep Power
    9400
    Also your SELECTs have to be named with []s.
    Code:
    <select multiple id="select2" name="select2[]">
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    393
    Rep Power
    47
    Thanks for your help...

    I now have the following code:
    PHP Code:
    if(isset($_POST['lesson_submit'])){
    echo 
    "Array:<br>";
    print_r($_POST['select2']) ;
    echo 
    "<br>Values:<br>";
    foreach(
    $_POST['select2'] AS $s)
    {
    echo 
    "$nbsp &nbsp Value: $s<br>";
    }
    echo 
    " -- end output --<br>"
    which produces the following results:
    Array:
    Array ( [0] => 2 [1] => 4 )
    Values:
    Value: 2
    Value: 4
    -- end output --
    The main problem was actually that my value assignment in the original code ("$value = $s " ; ) had quotes around it... I just didn't see that... duh!

    Works great now

    Thanks again for your input.

IMN logo majestic logo threadwatch logo seochat tools logo