Select Lists into MYSQL


Posted on 16th Feb 2014 07:03 pm by admin

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

Code: 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);

}




Code: 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()">&lt;</button>
<button onclick="addAll()">&lt;&lt;&lt;</button>
<button onclick="delAttribute()">&gt;</button>
<button onclick="delAll()">&gt;&gt;&gt;</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>


Code: <html><body>
<?php


$data = $_POST['selectedoptions'];


I AM LOST HERE



echo "Data Inserted!";






?>
</body></html>

No comments posted yet

Your Answer:

Login to answer
161 Like 9 Dislike
Previous forums Next forums
Other forums

Curl timeout breaks script
So im having difficulties with skipping timeout error in curl
my script calls different functions

Include with Parameters
In a particular page I would like to include a file that requires $_GET parameters.

when I go

fire away
i launched about 4 months ago as a lagit co. i have 2 paying clients, 1probono, 1 side progect, and

Parse Error with doctype
I'm getting a parse error with this simple code. I don't get it. It worked one time then when I relo

Passing vars to the DB
I have 3 small testing tables:

ID TYPE
1 pepperoni
2 C. bacon
3 taco

ID TY

Form help: Syntax & Logic
Hello again all,

I'm working on a form and ran into a wall (again) and can't seem to think th

Fatal error: Call to a member function fetchrow() on a non-object in C:xamppht
okay i have this query and everytime i add `item_id`=? to it... it gives me the error in the title..

Problem with variable declaration in switch statement
Hello, I am having some trouble assigning a value to a variable inside a switch statement. What I a

To change the name of label on SAP screen XK02.
Hi All,

Can one suggest me how to change the label of an input field of a sap standard s

Unable to display contents in Second Drop Down Box
Hi All,

What I am trying to do is 2 dependent drop down boxes and when user selects su

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash