sending items to db MySQL from the second select form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • richardsith
    New Member
    • Apr 2012
    • 5

    sending items to db MySQL from the second select form

    Hello everyone,
    I've a problem with 2 select form and MySQL.
    The first one lists all items, using 2 buttons I can transfer 'n remove items on the second form. The transfer works well, I'd have to record more idems(array) sent to second form on a db MySQL.
    If I select just 1 element I see it on db, but if the selection is more then 2 items I see only the last one selected.
    I don't know how to correct that, may you help me to resolve that,please.

    Below my files.

    index.php:

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script language="javascript">
    function getOpt(select1,select2)
     {
      for (bCnt=0;bCnt<select1.length;bCnt++)
       {
        if (select1.options[bCnt].selected)
         {
          newOpt=new
          Option(select1.options[bCnt].text,select1.options[bCnt].value,false,false);
          select2.options[select2.length]=newOpt;
         }
       }
     }
    
    function remOpt(select2)
     {
      for (bCnt=0;bCnt<select2.length;bCnt++)
       {
        if (select2.options[bCnt].selected)
        select2.options[bCnt]=null;
       }
     }
    </script>
    </head>
    <body>
    <form id="form" action="addring_mysql.php" method="post" name="form1">
    <fieldset>
    <legend id="legend_new">Add Items</legend>
    then the selects form:
    Code:
    <table border="0">
    <tr>
    <td>
    <label for="id">List:<br></label>
    <select name="oneS" id="select_role" size=20 required multiple="multiple"/>
    <option value="101">101</option>
    <option value="102">102</option>
    <option value="103">103</option>
    <option value="104">104</option>
    <option value="105">105</option>
    <option value="106">106</option>
    </select>
    </td>
    <td>
    <input type="button" value="Add"onClick="getOpt(this.form.oneS,this.for m.twoS)"><br>
    <input type="button" value="Remove"onClick="remOpt(this.form.twoS)">
    </td>
    <td>
    <label for="id">Members List:<br></label>
    <select name="twoS" id="select_role" size=20 multiple="multiple"/>
    </select>
    </td>
    </tr>
    </table>
      </fieldset>
        <p id="submit">
         <input type="submit" value="Save" />
        </p> 
    </form>
    </body>
    </html>
    addring_mysql.p hp:
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    
    <body onload="window.close()">
    
    <?php
    /* -------------------------------------------------------------------------- */
    require 'mysql_connect.php';
    require 'mysql_insert.php';
    /* -------------------------------------------------------------------------- */
    ?>
    </body>
    </html>
    mysql_insert.ph p:
    Code:
    <?php
    /* -------------------------------------------------------------------------  */
    $db_insert = mysql_query("INSERT INTO ring (id,ring,oneS) VALUES ('$id','$ring','oneS')") or die ("Error to insert the VoIP account to table users by query: " . mysql_error());
    /* -----
    thanks a lot in advance.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    If I select just 1 element I see it on db, but if the selection is more then 2 items I see only the last one selected.
    that’s correct because the values are overwriting each other. to pass multiple values under one name you have to add [] (array syntax) to the name.

    Comment

    • richardsith
      New Member
      • Apr 2012
      • 5

      #3
      I've already use that changing the form:

      <select name="oneS" id="select_role " size=20 required multiple="multi ple"/>
      in
      <select name="oneS[]" id="select_role " size=20 required multiple="multi ple"/>

      but, in this case, the javascript to transfer the items from first boxlist to second one dosen't work.
      If I remove the javascript, the second form, the buttons (>>,<<),use oneS[] and implode in MySQL I see the array recorded into table, but it's no my goal.
      I've to send the items transfered into second form to MySQL.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        but, in this case, the javascript to transfer the items from first boxlist to second one dosen't work.
        you have to adapt the script to reflect the new name. and since document.formna me.elementname[] has a different meaning you need to use document.formna me["elementnam e[]"].

        and implode in MySQL I see the array recorded into table, but it's no my goal.
        arrays should not be put into MySQL as a whole (serialised or not) anyways. you would need to normalize your DB and properly insert the data.

        Comment

        • richardsith
          New Member
          • Apr 2012
          • 5

          #5
          may you show me an example?
          thanks

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            example of what?

            Comment

            • richardsith
              New Member
              • Apr 2012
              • 5

              #7
              I need to know what have to change at the code to add the above command. I'm so sorry but I'm not an expert of javascript.

              Comment

              • richardsith
                New Member
                • Apr 2012
                • 5

                #8
                maybe this?
                <script>var name=document.f ormname["oneS[]"].value;</script>

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  the DB design is independent from JavaScript …

                  Comment

                  Working...