Link to home
Start Free TrialLog in
Avatar of corterp
corterp

asked on

Insert Selected or All List/Menu items into Database

I have a <div> that gets populated by the database.
      <?php
            echo ("<select multiple size=22 name='Sel' class='full Sel' style='width:375px;')'>");
                $sitename = $row_Recordset1["Title"];
            echo ("<option value='$sitename'>$sitename</option>");
                while ($row = mysql_fetch_array($Recordset1)) {
                    $sitename = $row["Title"];
                    echo ("<option value='$sitename'>$sitename</option>");
                }
            echo ("</select>")
      ?>

This works great, I get the list I desire.  Now I would like people to be able to select what they wish and Add via a button, also an Add All button if desired to add what they select into a separate section of the database set aside for their use.

Any help on how I can do this?  Sometimes the list to choose from will be as little as 1 to more then 300 items depending on their choice.

I have another box to list what is displayed but I can get that once I get the items they select into the database.

Thank you in advance.
Avatar of Sudaraka Wijesinghe
Sudaraka Wijesinghe
Flag of Sri Lanka image

You can do this in several ways;

1. since you are already using a multiselect element, you can let the user select multiple items with holding Ctrl/Shift keys and post that to the server

2. or you can use a second select element and use some client side scripting to move items between the two selects, then you post the second multiselect to the server.
Samples:
http://www.mredkj.com/tutorials/tutorial_mixed2b.html
http://www.iknowkungfoo.com/blog/index.cfm/2008/6/6/Move-options-between-Select-Menus

Note that in both cases you will have to name your multiselect with a name ending with [] to make the server side (PHP) treat it as an array (so you get all the selected items)
<select multiple size=22 name='Sel[]' class='full Sel' style='width:375px;'>

Open in new window


3. in addition to the above tow methods you can also use a widget like jQuery UI - multiselect to make it a bit fancy.
See the demo here
Avatar of corterp
corterp

ASKER

HI Sudaraka,

I am basically new to this.

This is what I have so far for the All All button.

<div id="AddAll">
  <form id="form1" name="form1" method="post" action="">
    <label>
      <input type="button" name="AddAll" id="AddAll2" value="Add All" onClick="addall" />
    </label>
  </form>
</div>
<script type="text/javascript">
      function addall() {
      <?php $L1 = $_SESSION['SelM1']; ?>
        <?php if ($L1 != '') { ?>
                  <?php
                        $array = Sel;
                        foreach ( $array as $value ) {
                              mysql_query("INSERT INTO PL (PLTitle, PLItem)
                              VALUES ('$L1', '$value')");
                              mysql_query("COMMIT");
                        }
                  ?>
            <?php } ?>
            }            
</script>

Nothing appears in the database and I fear adding when people select vice just clicking add all will be much more difficult.

Again thank you for your help.

ASKER CERTIFIED SOLUTION
Avatar of Sudaraka Wijesinghe
Sudaraka Wijesinghe
Flag of Sri Lanka image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial