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 8 of 8
  1. #1
    New Coder
    Join Date
    Mar 2012
    Location
    Ontario, Canada
    Posts
    54
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Saving Multiple values from Listbox

    Hello

    I have two listboxes on my form, one lists the items that they can select and move into the second listbox. I had no problems with that but now I want to save the values in the second listbox back to the table but don't want the user to select each of the values before they click save.

    Is there a way to get all of the values in the listbox without selecting the options?

    Code:
    if(!empty($_REQUEST['class2'])) {
      for($c=0; $c<count($_POST['class2']);$c++) {
          echo "the value is " . $_POST['class2'][$c] . "<br>";
      }
    }

  • #2
    New Coder
    Join Date
    Mar 2012
    Location
    Ontario, Canada
    Posts
    54
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Maybe the above can not be solved. Here is another solution that I am working on but can't find a way to add to the textbox.

    So I have a javascript that will move items from my first listbox into my second list box.

    PHP Code:
    if(mysql_num_rows($datclass) > 0) {
         echo 
    "<select multiple size='5' name='class1' style='width:275px'>";
        while(
    $df mysql_fetch_assoc($datclass)) {
            echo 
    "<option value='{$df['classify_id']}'>{$df['classify_type']}</option>";
        }
         echo 
    "</select>";
    }
    echo 
    "</td>";
    echo 
    "<td width='90px'>";
           echo 
    "<input type='button' onClick='move(this.form.class1,this.form.class2, this.form.fullArray)' value='->'><br />";
           echo 
    "<input type='button' onClick='move(this.form.class2,this.form.class1, this.form.fullArray)' value='<-'>";
    echo 
    "</td>";
    echo 
    "<td width='300px'>";
         if(
    mysql_num_rows($classify) > 0) {
        echo 
    "<select multiple size='5' name='class2' style='width:275px'>";
        while(
    $df mysql_fetch_assoc($classify)) {
             
    $select_array[] = $df['classify_id'];
             echo 
    "<option value='{$df['classify_id']}'>{$df['classify_type']}</option>";
        }
        echo 
    "</select>"
    this section will put the values from the second listbox into a text box so I can save the information.

    PHP Code:
    <input type='text' name='fullArray' value='" . implode(",", $select_array) . "'
    My problem now, is how to I change the values that are in the textbox when I move values from the first listbox to the second listbox. and remove values from the textbox when I move from second listbox to the first listbox.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Since you're using JS anyways, then you'll need to use JS to select all of the options within that second listbox prior to submitting. I believe you need to iterate all items in order to do so, but a simple JS function with an onsubmit should do the trick, or just automatically select them when adding. That will only really work if you make it readonly though.
    The problems of course is simply that PHP only receives what is selected from the checkbox. So if nothing is selected, than nothing is submitted. I'd still suggest keeping it in the select box instead of using an input for text.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    New Coder
    Join Date
    Mar 2012
    Location
    Ontario, Canada
    Posts
    54
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Sorry I am new to javascript and I don't know where I would put the javascript code to select all the values in the listbox before running my code to save the information.

    would I put a javascript on the onClick action for the button and will the POST code happen after?

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I'm also not a JS expert. I'd suggest that a new function for it and bind it to the onsubmit action on the form:
    Code:
    <!-- in the head somewhere, you should find an existing JS block -->
    function submitForm(selMenu)
    {
        var selectedBox = document.getElementById(selMenu);
        for (i = 0; i < selectedBox.options.length; ++i)
        {
            selectedBox.options[i].selected = true;
        }
    }
    
    <!-- then on the form -->
    <form action="yourscript.php" action="post" onsubmit="submitForm('selectedItems')">
    <select name="class2" id="selectedItems" multiple="multiple">
    ...
    Then in PHP:
    PHP Code:
    if (isset($_POST['class2']))
    {
        if (
    is_array($_POST['class2']))
        {
            
    printf("Selected items: %s" PHP_EOLimplode(', '$_POST['class2']));
        }

    And do whatever within the is_array check.
    Something along that lines; the client side guys can help more with setting it up if that doesn't work.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #6
    New Coder
    Join Date
    Mar 2012
    Location
    Ontario, Canada
    Posts
    54
    Thanks
    9
    Thanked 0 Times in 0 Posts
    I only get the last selected item from the list.

    The javascript is
    Code:
    function onSelect() {
         var classv = document.getElementById('listboxID');
         for(i=0; i<classv.options.length; i++) {
    	classv.options[i].selected = true;
         }
    }
    PHP code is

    Code:
    if(!empty($_REQUEST['class2'])) {
      for($i=0; $i< count($_POST['class2']); $i++) {
    	  echo "values are " . $_POST['class2'] . "<br>";
      }
    }
    Last edited by chellert; 03-11-2013 at 09:59 PM.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    That's why you need the JS to select all of them when you click the submit.
    If you want it to work in real(ish) time, you'll want to use AJAX for that. All and all, the concept would still be pretty much the same.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #8
    New Coder
    Join Date
    Mar 2012
    Location
    Ontario, Canada
    Posts
    54
    Thanks
    9
    Thanked 0 Times in 0 Posts
    i found the solution, unfortunately I had to write the values from the javascript to a hidden textbox on the form and then separate the values to save in the database.

    Might be a long way around but it works/

    thanks for your help


  •  

    Tags for this Thread

    Posting Permissions

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