gggggggg Posted December 13, 2009 Share Posted December 13, 2009 Hello All, I am new to PHP @ 1 week. So borrowing code anywhere I can. I am making progress, but hit a snag. I have 2 list boxes, that I use to move items left to right. When I click submit I want to load the items selected into MySQL. Is anyone able to help please? I can do the insert into MySQL (i think), but i cant work out how to get the values into a variable that PHP can use to submit. Then to make it more complicated, once I have it working for 1 list, I ideally wany about 10 lists on the page (with only 1 submit box). I am lost. Thanks greg This is my OPTION.js file var selectedList; var availableList; function createListObjects(){ availableList = document.getElementById("availableOptions"); selectedList = document.getElementById("selectedOptions"); } function delAttribute(){ var selIndex = selectedList.selectedIndex; if(selIndex < 0) return; availableList.appendChild(selectedList.options.item(selIndex)) selectNone(selectedList,availableList); setSize(availableList,selectedList); } function addAttribute(){ var addIndex = availableList.selectedIndex; if(addIndex < 0) return; selectedList.appendChild(availableList.options.item(addIndex)); selectNone(selectedList,availableList); setSize(selectedList,availableList); } function delAll(){ var len = selectedList.length -1; for(i=len; i>=0; i--){ availableList.appendChild(selectedList.item(i)); } selectNone(selectedList,availableList); setSize(selectedList,availableList); } function addAll(){ var len = availableList.length -1; for(i=len; i>=0; i--){ selectedList.appendChild(availableList.item(i)); } selectNone(selectedList,availableList); setSize(selectedList,availableList); } function selectNone(list1,list2){ list1.selectedIndex = -1; list2.selectedIndex = -1; addIndex = -1; selIndex = -1; } function setSize(list1,list2){ list1.size = getSize(list1); list2.size = getSize(list2); } function getSize(list){ /* Mozilla ignores whitespace, IE doesn't - count the elements in the list */ var len = list.childNodes.length; var nsLen = 0; //nodeType returns 1 for elements for(i=0; i<len; i++){ if(list.childNodes.item(i).nodeType==1) nsLen++; } if(nsLen<2) return 2; else return nsLen; } function showSelected(){ var optionList = document.getElementById("selectedOptions").options; var data = ''; var len = optionList.length; for(i=0; i<len; i++){ if(i>0) data += ','; data += optionList.item(i).value; } alert(data); } This is my edit.php file <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>Test</title> <script type="text/javascript" src="option.js"></script> </head> <body onload="createListObjects()"> <?php echo" <select name=\"selectedOptions[]\" id=\"selectedOptions\" multiple=\"true\"> "; //start the select box echo "<option value=\"1\">Cat</option>\n"; //and place it in the select echo "<option value=\"2\">Dog</option>\n"; //and place it in the select echo "</select>"; //close the select ?> <button onclick="addAttribute()"><</button> <button onclick="addAll()"><<<</button> <button onclick="delAttribute()">></button> <button onclick="delAll()">>>></button> </td> <?php echo" <select name=\"availableOptions\" id=\"availableOptions\" multiple=\"true\"> "; //start the select box echo "<option value=\"3\">pig</option>\n"; //and place it in the select echo "</select>"; //close the select ?> <tr> <td colspan="2"><button onclick="showSelected()"> Submit</button> </td> </tr> </body> </html> <html><body> <?php $data = $_POST['selectedoptions']; I AM LOST HERE echo "Data Inserted!"; ?> </body></html> Quote Link to comment https://forums.phpfreaks.com/topic/184952-select-lists-into-mysql/ Share on other sites More sharing options...
emopoops Posted December 13, 2009 Share Posted December 13, 2009 whats with the word snag? i just read it like three times in three different threads but this is the only one by you'? do u have multiple accounts or something? Quote Link to comment https://forums.phpfreaks.com/topic/184952-select-lists-into-mysql/#findComment-976505 Share on other sites More sharing options...
gggggggg Posted December 13, 2009 Author Share Posted December 13, 2009 heheheheheeh. I had snags for dinner. Quote Link to comment https://forums.phpfreaks.com/topic/184952-select-lists-into-mysql/#findComment-976646 Share on other sites More sharing options...
emopoops Posted December 14, 2009 Share Posted December 14, 2009 ????what does that mean? r u thos toher accounts? Quote Link to comment https://forums.phpfreaks.com/topic/184952-select-lists-into-mysql/#findComment-976665 Share on other sites More sharing options...
gggggggg Posted December 14, 2009 Author Share Posted December 14, 2009 sorry, those other accounts are nothing to do with me. Quote Link to comment https://forums.phpfreaks.com/topic/184952-select-lists-into-mysql/#findComment-976668 Share on other sites More sharing options...
emopoops Posted December 14, 2009 Share Posted December 14, 2009 ok ;] Quote Link to comment https://forums.phpfreaks.com/topic/184952-select-lists-into-mysql/#findComment-976721 Share on other sites More sharing options...
mikesta707 Posted December 14, 2009 Share Posted December 14, 2009 you can get the value of a select box the same way you get the value of any submitted input tag. just use its name attribute as the index in the POST/GET array //get the value of the available options value $options = $_POST['availableOptions']; Quote Link to comment https://forums.phpfreaks.com/topic/184952-select-lists-into-mysql/#findComment-976760 Share on other sites More sharing options...
gggggggg Posted December 14, 2009 Author Share Posted December 14, 2009 Thats what I though also, but I treid this, and it didnt work: //get the value of the available options value $options = $_POST['availableOptions']; echo $options ; It told me: Notice: Undefined index: availableOptions in xxxxxxxxxxxx Line 7 I have attached my source, mayby that will help. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/184952-select-lists-into-mysql/#findComment-976792 Share on other sites More sharing options...
gggggggg Posted December 15, 2009 Author Share Posted December 15, 2009 any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/184952-select-lists-into-mysql/#findComment-977668 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.