Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2002
    Location
    Malaysia
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Multiple values in one listbox to a textbox

    Hi, I have a form which has a listbox and a textbox. I named the listbox as fruit[] as I want to select multiple values in it rather than one. When I control-click on the listbox to select multiple values, I wish to show what I have selected in the listbox to the textbox on the same form...something like:

    banana, pineapple, watermelon, strawberry....

    I am able to show this in PHP (but not into the textbox, just echo only)...the code is like this:

    PHP Code:
    if (isset($_POST[Submit])) {
    $Fruit $_POST['Fruit'];

    foreach (
    $Fruit as $Fruit1) {

    $Fruit1 $Fruit1 "," " ";
    echo 
    "$Fruit1\n";

    }

    I am not good in array kind of thing, somemore in Javascript. Hope someone can help me on this, thanks!

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Like this?
    Code:
    <script type="text/javascript">
    function showData(sel){
       var str='';
       for (var i=0;i<sel.options.length;i++){
           if (sel.options[i].selected){
               str=(str!='') ? ', '+sel.options[i].value : sel.options[i].value;
           }
       }
       sel.form.selectedFruits.value = str;
    }
    </script>
    ...
    <form>
    <select name="fruit[]" size="5" multiple="multiple" onchange="showData(this)">
    <option value="banana">banana</option>
    <option value="pineapple">pineapple</option>
    <option value="watermelon">watermelon</option>
    </select>
    <input type="text" name="selectedFruits" />
    </form>
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    New Coder
    Join Date
    Aug 2002
    Location
    Malaysia
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks mate, I think it is close...

    Testing shows that if you select banana and pineapple for instance, it shows only .pineapple rather than banana, pineapple.

    I wonder where the dot comes from...so I look at this line of code:

    str=(str!='') ? ', '+sel.options[i].value : sel.options[i].value;

    Not too sure what it does, but I will keep trying (to debug).

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    You might be mistaking comma to be a dot or you have a dot in the option value?

    In any case, I saw where the error is. I forgot the + operator.

    str+=(str!='') ? ', '+sel.options[i].value : sel.options[i].value;
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #5
    New Coder
    Join Date
    Aug 2002
    Location
    Malaysia
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks very much! Now it works!

    I am also puzzled by that dot, I am sure it is a comma in your line of code...this shows the beauty of programming


  •  

    Posting Permissions

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