View Full Version : PHP multi-select list and javascript problem
kramersinva
03-16-2006, 06:24 PM
Hello,
I have a multi-select listbox created (and processed via PHP). As such I named it with braces in the name, like:
<select name="multilist[]" size=10 multiple>
However, I can't get javascript to reference the listbox when I name it that way (I need the javascript to dynamically insert/delete items from the list).
I had originally named the list "multilist" without the braces and the javascript worked fine, and it let me multi-select, but php would only see the last item selected. Based on hints I read elsewhere, to enable php to process the multi-selections into an array, I renamed the list with the braces [] but now the javascript simply does not work -- it will not insert data into the list.
Is their any trick to the control naming I need to know?
Thanks... the browser is Safari on OSX if that makes a difference for the javascript.
bustamelon
03-16-2006, 06:40 PM
You might try posting this in the javascript formum too, and maybe someone will have a workaround for the javascript side of things. Actually, if you paste the code here, it may help too. AFAIK, there is no way to get PHP to properly handle a multiple select without using the empty index []. But perhaps you can reference the element differently in your javascript. Since you didn't paste any code, I have to guess here, but you are probably referencing the select by name (multilist)? What about getElementByID() ?
<select name="multilist[]" id="multilist" size=10 multiple>
Then your javascript would reference the select list something like this:
MySelect = document.getElementById(multilist);
Whoops. I just read the guidelines, and you're not allowed to cross-post.... Oh well.
kramersinva
03-16-2006, 09:51 PM
bustamelon,
Thanks for pointing me in the right direction. I had indeed been referencing the controls by name, and the braces [] seemed to mess that up. I converted everything to getElementById and all is working again.
Thanks!
kramersinva
bustamelon
03-16-2006, 10:30 PM
You're welcome.
Sohail Abbas
09-07-2006, 08:27 AM
// ==========================================
This is javascript code
// mredkj.com
// created: 2001-07-11
// last updated: 2001-09-07
var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);
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[i].selected)
{
selectedText[selectedCount] = theSelFrom.options[i].text;
selectedValues[selectedCount] = theSelFrom.options[i].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[i], selectedValues[i]);
}
if(NS4) history.go(0);
}
// ==============================================
This is html control
<table width="99%" border="0">
<tr>
<td width="45%">
<select multiple size="10" name="sel1">
<option value="155101">Auktioner</option>
<option value="132901">Barn & Baby</option>
<option value="132901-100140113">-Baby på vej</option>
<option value="132901-108501">--Babytøj</option>
<option value="132901-100328023">--Babyudstyr</option>
</select>
</td>
<td width="9%">
<input onclick="moveOptions(this.form.sel1, this.form.sel2);" type=button value=" » "><br/>
<input onclick="moveOptions(this.form.sel2, this.form.sel1);" type=button value=" « ">
</td>
<td width="45%">
<select multiple size="10" name="sel2[]"> </select>
</td>
</tr>
</table>
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.