Location:
Posts: 34
Javascript arrays []
I have researched my problem, but I am stuck on how to accomplish what I am trying to do. I have two list boxes. One of all none selected elements and one with current selected elements. I want to give the user the ability of adding and removing elements from the list boxes, however, I don’t want constant I/O for something that should be done on the client side. I found the JavaScript code that adds and removes elements from the different list boxes, but the way php pass multiple selections (i.e. addFacList[]) is giving me problems. I read that in order interact with arrays and JavaScript to do this:
variable = document.forms[0].elements[‘var[]’];
But not being that proficient with JavaScript could someone give me a hand with what I need to do and/or doing wrong… My code is as follows… Thanks for any help or suggestions....
<script language="javascript">
<!--
function addOption(theSel, theText, theValue)
{
var newOpt = new Option(theText, theValue);
var selLength = theSel.length;
theSel.options[selLength] = newOpt;
}
function deleteOption(theSel, theIndex)
{
var selLength = theSel.length;
if(selLength>0)
{
theSel.options[theIndex] = null;
}
}
function moveOptions(theSelFrom, theSelTo)
{
var selLength = theSelFrom.length;
var selectedText = new Array();
var selectedValues = new Array();
var selectedCount = 0;
var i;
// Find the selected Options in reverse order
// and delete them from the 'from' Select.
for(i=selLength-1; i>=0; i--)
{
if(theSelFrom.options.selected)
{
selectedText[selectedCount] = theSelFrom.options.text;
selectedValues[selectedCount] = theSelFrom.options.value;
deleteOption(theSelFrom, i);
selectedCount++;
}
}
// Add the selected text/values in reverse order.
// This will add the Options to the 'to' Select
// in the same order as they were in the 'from' Select.
for(i=selectedCount-1; i>=0; i--)
{
addOption(theSelTo, selectedText, selectedValues);
}
}
//-->
</script>
print("<select name='removeFacList' size='10' multiple>");
$select ="SELECT ann_fac.fac_cd, fac_sdesc ";
$from ="FROM facility, ann_fac ";
$where ="WHERE ann_srl='$selected'";
$where .="AND ann_fac.fac_cd!=facility.fac_cd ";
$facQuery=$select.$from.$where;
$facilityData=db_query($facQuery);
while(list($fac_cd, $fac_sdesc)=db_fetch_row($facilityData)) {
print("<option value='$fac_cd'>$fac_sdesc</option>");
}
print("</select>");
print("<input type='button' value='--->' onClick='moveOptions(this.form.removeFacList, this.form.addFacList[]);' /><br><br />");
print("<input type='button' value='<---' src='images/remove.gif' onClick='moveOptions(this.form.addFacList[], this.form.removeFacList);' />");
print("<select name='addFacList[]' size='10' multiple>");
$select ="SELECT ann_fac.fac_cd, fac_sdesc ";
$from ="FROM facility, ann_fac ";
$where ="WHERE ann_srl='$selected'";
$where .="AND ann_fac.fac_cd=facility.fac_cd ";
$facQuery=$select.$from.$where;
$facilityData=db_query($facQuery);
while(list($fac_cd, $fac_sdesc)=db_fetch_row($facilityData)) {
print("<option value='$fac_cd'>$fac_sdesc</option>");
}
print("</select>");