-2

I have a dilemma which I cannot seem to overcome. The scenario is I have 2 select list boxes #boxdest & #boxdest2.

The aim is to populate #boxdest2 with items from #boxdest which it does successfully. However, when i send to results page for processing, #boxdest is coming through instead of #boxdest2 which holds the items. Funny thing is that #boxdest appears to be sending the correct data through which is actually in #boxdest2.

I would be grateful if someone could point out where I have gone wrong. Thanks

<?php
    $conn = mysql_connect("localhost","root","");
    mysql_select_db("sample",$conn); 
    $result = mysql_query("SELECT * FROM boxes where department = '{$_GET['dept']}' ORDER BY custref ASC");
?>

    <select name="boxdest[]" id="boxdest" size="7" multiple="multiple">

<?php
    $i=0;
    while($row = mysql_fetch_array($result)) {
?>
    <option value="<?php echo $row["custref"];?>"><?php echo $row["custref"];?></option>
<?php
    $i++;
    }
?>
    </select>
          <span style="display: inline-block; width: 70px; height: 82px; padding-left: 10px; padding-right: 10px; vertical-align: top;margin-top:35px;">
          <input type="button" id="submit2" name="submit2" value=">" />
           <input type="button" id="submit3" name="submit3" value="<" />
       </span>
       <select name="boxdest2[]" id="boxdest2" size="7" multiple="multiple"></select>

jquery code

<script type="text/javascript">

    $(document).ready(function () {
  // initial list, as fetched from the server
  var initialList = $('#boxdest > option')
    .map(function () { return this.value; }).get();

  /**
   * @param {string} source
   * @param {string} destination
   */
  function exchangeLists(source, destination) {
    // find all selected items on the source list
    var selected = $(source + ' > option:selected');

    // move them to the destination list
    $(destination).append(selected.clone());

    // remove from the source list
    selected.remove();

    // sort the destination list
    var list = $(destination + ' > option').clone().sort(function (a, b) {
      if (initialList.indexOf(a.value) < initialList.indexOf(b.value)) {
        return -1;
      }

      if (initialList.indexOf(a.value) > initialList.indexOf(b.value)) {
        return 1;
      }

      return 0;
    });

    // replace current destination list with the sorted one
    $(destination).empty().append(list);
  }

  $('#submit2').click(function () {
    exchangeLists('#boxdest', '#boxdest2');
  });

  $('#submit3').click(function () {
    exchangeLists('#boxdest2', '#boxdest');
  });
});
</script>
user1532468
  • 1,723
  • 8
  • 41
  • 80
  • 3
    Your code is vulnerable to [**SQL injection attacks**](https://en.wikipedia.org/wiki/SQL_injection). You should use [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) prepared statements with bound parameters as described in [**this post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Alex Howansky Apr 14 '17 at 14:41
  • 3
    Stop using the `mysql_*` functions. They have been deprecated since v5.5 (Jun 2013) and removed since v7.0 (Dec 2015). Instead use the [**mysqli_***](https://secure.php.net/manual/en/book.mysqli.php) or [**PDO**](https://secure.php.net/manual/en/book.pdo.php) functions with [**prepared statements**](https://secure.php.net/manual/en/pdo.prepare.php) and [**bound parameters**](https://secure.php.net/manual/en/pdostatement.bindparam.php). – Alex Howansky Apr 14 '17 at 14:41
  • I am aware of that Alex. However, as I am only working locally, not relevant. How does that help with my question? – user1532468 Apr 14 '17 at 14:43
  • As other have pointed out, definitely check out sites like http://www.phptherightway.com/ – user49438 Apr 14 '17 at 15:01

1 Answers1

1

Are you making sure the items are selected after they move between the boxes? As is, when you shift items from the left to the right, they aren't selected after the move so they are not posted. If you then re-select the items in each box before submitting the form (for example, by holding shift and clicking them), it does post them correctly.

user49438
  • 889
  • 7
  • 20
  • 49438. Sorry but I do not understand your response. How can I select them again after they are removed from listbox1. I can see that they are being moved to the second listbox. Thanks – user1532468 Apr 14 '17 at 15:07
  • Your code pretty worked for me when I tried it, but I noticed that when items are moved from the first list to the second list, they don't stay selected and therefore are not posted back. After moving some items to the second list, select those items in the second list by holding shift and clicking them, then submit the form. Otherwise when the form is posted back, nothing is selected and therefore the items are not in `$_POST`. You may want to change your jQuery so that after items are moved to the second list, everything in the second list is selected. – user49438 Apr 14 '17 at 17:40
  • Thanks for response. My question thought is, why would it behave that way? any ideas gratefully recieved – user1532468 Apr 15 '17 at 08:35