An item is selected from a list and then used as a variable in a PHP script to identify and retrieve some data from a specific record in a MySQL database.
Here's the code for the select script (first a javascript external file, then an html file) :
/* This is placed in an external file, addingOptions.js */
var otherStuff = {
"item 1" : [ "subitem 1.1", "subitem 1.2", "subitem 1.3", "subitem 1.4" ],
"item 2" : [ "subitem 2.1", "subitem 2.2" ],
"item 4" : [ "subitem 4" ],
"item 6" : [ "subitem 6.1", "subitem 6.2" ]
};
function selectAll(listName, selected) {
var listBox = document.getElementById(listName);
for(i=0; i<listBox.length; i++) {
listBox.options[i].selected=selected;
}
if( listBox.onchange ) {
listBox.onchange();
}
}
function lstStuff_OnChange() {
var listBox = document.getElementById("lstStuff");
var subListBox = document.getElementById("lstOtherStuff");
subListBox.options.length=0;
for(i=0; i<listBox.length; i++) {
if( listBox.options[i].selected ) {
var key = listBox.options[i].text;
if(otherStuff[key]) {
for(j=0; j<otherStuff[key].length; j++) {
subListBox.options.add(new Option(otherStuff[key][j],otherStuff[key][j]));
}
}
}
}
}
Here's the main .html code/file:
<html>
<head>
<script type="text/javascript" src="addingOptions.js"></script>
</head>
<body>
<form>
<select id="lstStuff" multiple="multiple" onChange="lstStuff_OnChange()" size="6" style="width:200px;">
<option>item 1</option>
<option>item 2</option>
<option>item 3 (No Sub-Items)</option>
<option>item 4</option>
<option>item 5 (No Sub-Items)</option>
<option>item 6</option>
</select>
<br>Select:
<a href="javascript:selectAll('lstStuff', true);">all</a> |
<a href="javascript:selectAll('lstStuff', false);">none</a>
<p>
<select id="lstOtherStuff" multiple="multiple" size="6" style="width:200px;">
</select>
<br>Select:
<a href="javascript:selectAll('lstOtherStuff', true);">all</a> |
<a href="javascript:selectAll('lstOtherStuff', false);">none</a>
</form>
</body>
</html>
URL for script above:
http://javascript.internet.com/forms/adding-options-2.html
You'll notice that this produces two select boxes, one with categories and one with subcategories. I'd like to be able to store the selection of a subcategory (by an end user) into a variable (I'm guessing a global variable, since I want to pass it on to a PHP script) and then take that variable and use it in a PHP script, within the main html file, in order to identify a particular record within a MySQL database.
How do I do this?
Any assistance will be greatly appreciated and I'll be happy to publish the final complete code once I get this thing to work.