0

EDIT : PLEASE FIND MY SOLUTION BELLOW

I have two select boxes in a FORM that i populate from two different tables. SELECT1 displays client specific options. SELECT2 displays all available options except those already on SELECT1

Everything works fine populating them, moving items from one to the other etc.

What I don't have a clue is how to, after submit of the FORM, I store those items of SELECT1 back to the database. For each item on that SELECT box I need to check if it already exists on MY_TABLE_2 and if it does not, store it.

When I check var_dump(SELECT1) I only see the value of the last selected item but it normally contains more than one item in SELECT1.

For now I'm calling a stored procedure for all the other fields that will go on MY_TABLE_1 and that is OK. I would need to do some transaction that updates the two tables or rolls back if i get an error. That is working if i store only one item.

But how I send to the stored procedure ALL the items.

Any help or clues?

Here's a print-screen if it can help understand:

https://i.stack.imgur.com/CTkSZ.png

The code to generate the form:

echo '<select name="ed_clilabelcodes" id="ed_clilabelcodes" style="width: 200px" multiple>';
while($row = sqlsrv_fetch_array($dvclilablist)){
  echo '<option value="'.$row[0].'">'.$row[0].' - '.$row[1].'</option>';
}
echo '</select>';

----------- MY SOLUTION -----------

All items on any of those select boxes exist in a table (dbo.MyItems) identified by the field OptCode. What I really needed was to store witch of those items were selected by a particular client. I was thinking of having a second table where I would store those items but I was in reality duplicating information and I didn't find it to be the best solution.

Since I really only needed those selected codes attached to the client file I did the following:

I have one javascript function inspired from HERE that allows me to move items between the two select boxes and, by the way, from HERE to create a function that sorts the items in the boxes. Each time I add or remove an item from the client's list box I immediately call the sort function, so I just update a hidden input value with all item values (codes) present in the select box immediately calling this javascript function :

function RecheckCodes() {
  var vlist = document.getElementById("ed_clilabelcodes");
  var vlabcodes = "";
  var i;
  for (i = 0; i < vlist.length; i++) {
    if (i == 0) {
      vlabcodes = vlabcodes + "\'" + vlist.options[i].value + "\'";
    }
    else {
      vlabcodes = vlabcodes + ",\'" + vlist.options[i].value + "\'";
    }
  }
  document.getElementById("v_clilabcodes").value = vlabcodes;
}

On FORM Post i just save that string in a client field.

Latter i can repopulate the boxes for edit using following 2 stored procedures to get the items (first SQL will get the string with the codes and is called from the 2nd one that gets the item description from items table).

ALTER PROCEDURE [dbo].[DavGetOptCodeByClient] 
  @vclicode nvarchar(12),
  @vopttype nvarchar(12),
  @voptcodes nvarchar(300) OUTPUT
AS
BEGIN
  SET NOCOUNT ON;
  SELECT @voptcodes = optcodes
  FROM   dbo.dav_client_options
  WHERE  clinumber = @vclicode AND opttype = @vopttype
END



ALTER PROCEDURE [dbo].[DavGetOptDetailByClient] 
  @vclicode nvarchar(12),
  @vopttype nvarchar(12)
AS
BEGIN
  SET NOCOUNT ON;
  DECLARE @v2optcodes nvarchar(1000)
  DECLARE @vsql nvarchar(1000)
  exec dbo.DavGetOptCodeByClient @vclicode,@vopttype, @v2optcodes OUTPUT
  SET @vsql = 'SELECT optcode, optdetail ' +
            'FROM dbo.dav_global_options ' +
            'WHERE ("optcode" IN ('+@v2optcodes+')) AND (opttype = '''+@vopttype+''')'
  EXEC sp_executesql @vsql
END

So everything is working fine for what I needed even if I'm sure some improvement can be made on the code.

  • You could possibly use a table valued parameter, or the less optimal way would be to call your procedure for every item in the list. – Sean Lange Mar 13 '18 at 14:40
  • An imaga may say more than a thousand words. May. In this case I'd prefer the php that generates the form and the php that handles the posted data. – Gerard H. Pille Mar 13 '18 at 14:41
  • Maybe you may duplicate all items in two tables adding a quantity column to both tables. Then you may change quantity only - it should be easier. Also you may use only one table for this task adding Select# column to it. If both select can't have same value, you may simple change Select# ID for each value. – toor Mar 13 '18 at 14:46
  • @GerardH.Pille, the php that will handle passing those 3 elements in the "Client labels" box to the stored procedure is the one I'm also looking for. All the rest is working fine. I was playing with the idea of using the javascript i have to move items from a box to the other and have it give all the item values to a var, separated by commas and then send it to the SQL stored proc but is there a way to then split it to each individual code and read each element on the "all labels" table and insert them on MY_TABLE_2? – Paulo Silva Mar 13 '18 at 15:02
  • Delimited data is possible. What version of sql server are you using? – Sean Lange Mar 13 '18 at 15:16
  • @PauloSilva A html form would already be sending you those labels in an array, that's why I wanted to see the php that generates the form or at least the resulting HTML. – Gerard H. Pille Mar 13 '18 at 15:22
  • @SeanLange, it's vers 2012 and express(win10) for testing @GerardH.Pille, here is the select : `echo '';` The rest of the form is pretty standard... On Post, i send all other text inputs to the SQL stored proc. Only problem is sending all the values of these items... – Paulo Silva Mar 13 '18 at 15:55
  • Code in the comments is pretty awful because the formatting is destroyed. You either need to pass this as a table parameter or split your strings. – Sean Lange Mar 13 '18 at 15:57
  • @SeanLange I've edited the question and added the php code. Just wait for a review. – Gerard H. Pille Mar 13 '18 at 18:33
  • @PauloSilva Are you looking for help with the php to receive the selected options, or to code to update your database. In the first case, put a phpinfo() as first command of the php page that receives the form, you will immediately see how you can retrieve the transmitted information. – Gerard H. Pille Mar 13 '18 at 18:35
  • @GerardH.Pille, i was looking for the php code to send each item value to the stored proc and how would the sql be in the stored proc to handle that. After many research i found a solution to my problem that suits me and will edit the question with it. Maybe it can be useful for someone else. – Paulo Silva Mar 14 '18 at 08:31

1 Answers1

0

to be able to send all the elements the simplest option is to select them, it is not enough that they are in the list but they have to be selected, for that it must be activated the "multiple" option of the select, once done that by pressing the key "control" or "shift" you can make multiple select in your list.

<form action="#" method="post">
        <select name="Color[]" multiple> <!--the option multiple must be active and be array -->
            <option value="Red">Red</option>
            <option value="Green">Green</option>
            <option value="Blue">Blue</option>
            <option value="Pink">Pink</option>
            <option value="Yellow">Yellow</option>
        </select>
        <input type="submit"  value="Get Selected Values" />
    </form>

Another option is to add this small javascript (jQuery) in an html so you do not have to press or control or shift when you select several options, just click and you will be selected

$('option').mousedown(function(e) {
    e.preventDefault();
    $(this).prop('selected', !$(this).prop('selected'));
    return false;
});

Source :How to avoid the need for ctrl-click in a multi-select box using Javascript?

And at the end you have your data with a simple php

<?php
$data=$_POST;
if($data) {
    foreach ($data['Color'] as $option) {
        echo $option;
    }
    var_dump($data);
}
?>
eborrallo
  • 750
  • 1
  • 8
  • 17