mmr mmr - 8 days ago 6
PHP Question

Get selected values in a dual listbox PHP

I want to output the selected values from the right hand listbox. However I am not successful in getting the values of selected values. The error returns from the submit function of js. Can anyone tell me why does it return:

An object doesn't support this property


in the submit function? How can i get the values.. will i use ajax or just returned value in PHP?

<form name="frmListSubmit" method="post" id="myform" onsubmit="return submit()">

<table cellpadding="2" cellspacing="0" border="0" id="tblMain" class="edit">
<tr valign="top">
<td>&nbsp;&nbsp;&nbsp;</td>
<td>
<SELECT id="s" size="10" name="source" multiple>
<?php
while($row = mysql_fetch_assoc($getEmployeeList))
{
?>
<option name="list" value="<?php echo $row['id'];?>"><?php echo $row['name']; ?></option>
<?php
}
?>

</td>
</SELECT>

<td valign="center">
<a href="#" onclick="listbox_moveacross('s', 'd')" style="color:#FFF">&gt;&gt;</a>
<a href="#" onclick="listbox_moveacross('d', 's')" style="color:#FFF">&lt;&lt;</a>
</td>
<td>
<SELECT id="d" size="10" name="destination" multiple>
<option name="list" value="">&nbsp;</option>
</SELECT>
<input type="submit" name="submit" value="submit" />
</td>
</tr>
</table>

</form>


and here is the javascript code:

function listbox_moveacross(sourceID,destID)
{
var src=document.getElementById(sourceID);
var dest=document.getElementById(destID);
for(var count=0;count<src.options.length;count++)
{
if(src.options[count].selected==true)
{
var option=src.options[count];
var newOption=document.createElement("option");
newOption.value=option.value;
newOption.text=option.text;
newOption.selected=true;
try
{
dest.add(newOption,null);src.remove(count,null);
}
catch(error)
{
dest.add(newOption);src.remove(count);
}

count--;
}
}
}

function listbox_selectall(listID,isSelect)
{
var listbox=document.getElementById(listID);
for(var count=0;count<listbox.options.length;count++)
{
istbox.options[count].selected=isSelect;
}
}

function submit()
{
listbox_selectall('d', true);
return true;
}


I have something like this in PHP and it returns only one ID after submission.

if(isset($_POST['submit']))
{
echo $_POST['destination'];
}

Answer

This is the weird code:

function listbox_selectall(listID,isSelect)
{
    var listbox=document.getElementById(listID);
    for(var count=0;count<listbox.options.length;count++)
    {
        istbox.options[count].selected=isSelect;//<-- ?
    }
}

Maybe, you wanted to say:

listbox.options[count].selected=isSelect;

You said, that only 1 value from "destination" is submitted to PHP. To allow multiple values to be submitted, add [] to the names of the <select>-s:

<SELECT id="s" size="10" name="source[]" multiple>

<SELECT id="d" size="10" name="destination[]" multiple>

Also, there is no need to use name attribute for options. You don't use it anywhere.