By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,039 Members | 1,831 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,039 IT Pros & Developers. It's quick & easy.

sending items to db MySQL from the second select form

P: 5
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:

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <script language="javascript">
  6. function getOpt(select1,select2)
  7.  {
  8.   for (bCnt=0;bCnt<select1.length;bCnt++)
  9.    {
  10.     if (select1.options[bCnt].selected)
  11.      {
  12.       newOpt=new
  13.       Option(select1.options[bCnt].text,select1.options[bCnt].value,false,false);
  14.       select2.options[select2.length]=newOpt;
  15.      }
  16.    }
  17.  }
  18.  
  19. function remOpt(select2)
  20.  {
  21.   for (bCnt=0;bCnt<select2.length;bCnt++)
  22.    {
  23.     if (select2.options[bCnt].selected)
  24.     select2.options[bCnt]=null;
  25.    }
  26.  }
  27. </script>
  28. </head>
  29. <body>
  30. <form id="form" action="addring_mysql.php" method="post" name="form1">
  31. <fieldset>
  32. <legend id="legend_new">Add Items</legend>
  33.  
then the selects form:
Expand|Select|Wrap|Line Numbers
  1. <table border="0">
  2. <tr>
  3. <td>
  4. <label for="id">List:<br></label>
  5. <select name="oneS" id="select_role" size=20 required multiple="multiple"/>
  6. <option value="101">101</option>
  7. <option value="102">102</option>
  8. <option value="103">103</option>
  9. <option value="104">104</option>
  10. <option value="105">105</option>
  11. <option value="106">106</option>
  12. </select>
  13. </td>
  14. <td>
  15. <input type="button" value="Add"onClick="getOpt(this.form.oneS,this.for m.twoS)"><br>
  16. <input type="button" value="Remove"onClick="remOpt(this.form.twoS)">
  17. </td>
  18. <td>
  19. <label for="id">Members List:<br></label>
  20. <select name="twoS" id="select_role" size=20 multiple="multiple"/>
  21. </select>
  22. </td>
  23. </tr>
  24. </table>
  25.   </fieldset>
  26.     <p id="submit">
  27.      <input type="submit" value="Save" />
  28.     </p> 
  29. </form>
  30. </body>
  31. </html>
  32.  
addring_mysql.php:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. </head>
  6.  
  7. <body onload="window.close()">
  8.  
  9. <?php
  10. /* -------------------------------------------------------------------------- */
  11. require 'mysql_connect.php';
  12. require 'mysql_insert.php';
  13. /* -------------------------------------------------------------------------- */
  14. ?>
  15. </body>
  16. </html>
  17.  
mysql_insert.php:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /* -------------------------------------------------------------------------  */
  3. $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());
  4. /* -----
  5.  
thanks a lot in advance.
Apr 23 '12 #1
Share this Question
Share on Google+
8 Replies


Dormilich
Expert Mod 5K+
P: 8,639
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.
Apr 23 '12 #2

P: 5
I've already use that changing the form:

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

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.
Apr 23 '12 #3

Dormilich
Expert Mod 5K+
P: 8,639
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.formname.elementname[] has a different meaning you need to use document.formname["elementname[]"].

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.
Apr 23 '12 #4

P: 5
may you show me an example?
thanks
Apr 23 '12 #5

Dormilich
Expert Mod 5K+
P: 8,639
example of what?
Apr 23 '12 #6

P: 5
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.
Apr 23 '12 #7

P: 5
maybe this?
<script>var name=document.formname["oneS[]"].value;</script>
Apr 23 '12 #8

Dormilich
Expert Mod 5K+
P: 8,639
the DB design is independent from JavaScript …
Apr 23 '12 #9

Post your reply

Sign in to post your reply or Sign up for a free account.