Jump to content

Multiple Selection Lists & PHP


ShibSta

Recommended Posts

Ok, I have 2 multiple selection lists. I am using javascript so that users can select items from list 1 and move them to list 2. When the user submits the form I need my php to get the values he has/she has in list 2...

 

I have done printf($_POST['list_2']) and it only shows one value, even when multiple are selected. I read that I need to change it's name to list_2[] but if I add the brackets to the name my javascript quits working...

 

Any suggestions?

Link to comment
https://forums.phpfreaks.com/topic/43805-multiple-selection-lists-php/
Share on other sites

Sorry for double posting here but I can't find the edit button. ><

 

Here is my JS/HTML, my PHP is simply printf($_POST['send_to']);. I guess this is kinda more of a JS issue but if there is a PHP fix or if anyone knows how to fix my JS, I'd appreciate it. Thanks.

 

<script type="text/javascript">
<!--
function compareOptionValues(a, b)
	{
	var sA = parseInt(a.value, 36);
	var sB = parseInt(b.value, 36);
	return sA - sB;
	}
function compareOptionText(a, b)
	{
	var sA = parseInt(a.text, 36);
	var sB = parseInt(b.text, 36);
	return sA - sB;
	}
function moveDualList(srcList, destList, moveAll)
	{
	if ((srcList.selectedIndex == -1) && (moveAll == false))
		{
		return;
		}
	newDestList = new Array(destList.options.length);
	var len = 0;
	for (len = 0; len < destList.options.length; len++)
		{
		if (destList.options[len] != null)
			{
			newDestList[len] = new Option(destList.options[len].text, destList.options[len].value, destList.options[len].defaultSelected, destList.options[len].selected);
			}
		}
	for (var i = 0; i < srcList.options.length; i++)
		{
		if (srcList.options[i] != null && (srcList.options[i].selected == true || moveAll))
			{
			newDestList[len] = new Option(srcList.options[i].text, srcList.options[i].value, srcList.options[i].defaultSelected, srcList.options[i].selected);
			len++;
			}
		}
	newDestList.sort(compareOptionValues);
	for (var j = 0; j < newDestList.length; j++)
		{
		if (newDestList[j] != null)
			{
			destList.options[j] = newDestList[j];
			}
		}
	for (var i = srcList.options.length - 1; i >= 0; i--)
		{
		if (srcList.options[i] != null && (srcList.options[i].selected == true || moveAll))
			{
			srcList.options[i] = null;
			}
		}
	}
	//-->
</script>


			<table cellpadding="0" cellspacing="0" border="0">
				<tr>
					<td width="150" valign="top">
						<select name="list" style="width:150px; height:100px; font-size:10px" multiple="multiple" size="6" ondblclick="moveDualList( this.form.list,  this.form["send_to[]"][0], false )">
							<!-- BEGIN user_list -->
							<option value='{UID}'>{UID} - {USERNAME}</option>
							<!-- END user_list -->
						</select>
										</td>
					<td width="150" align="center" valign="middle">
						<input style="width: 120px; margin: 2px 0 2px 0; font-size: 10px;" type="button" value="Add Selected  »" onclick="moveDualList( this.form.list, this.form["send_to[]"][0], false )"/>
						<input style="width: 120px; margin: 2px 0 2px 0; font-size: 10px;" type="button" value="«  Remove Selected" onClick="moveDualList( this.form["send_to[]"][0], this.form.list, false )"/>
						<input style="width: 120px; margin: 2px 0 2px 0; font-size: 10px;" type="button" value="Add All  »" onClick="moveDualList( this.form.list, this.form["send_to[]"][0], true )"/>
						<input style="width: 120px; margin: 2px 0 2px 0; font-size: 10px;" type="button" value="«  Remove All" onClick="moveDualList( this.form["send_to[]"][0], this.form.list, true )"/>
					</td>
					<td valign="top">
						<select name="send_to[]" style="width:150px; height:100px; font-size:10px" multiple="multiple" size="6" onDblClick="moveDualList( this.form["send_to[]"][0], this.form.list,  false )">
						</select>
					</td>
				</tr>
			</table>

 

Edit: Ok, there must be a time limit on how long you have to modify a post, I see the link on this topic... :/

Javascript problem, 2 ways to fix

 

1 ) Give the form element an id and use myselect = document.getElementById(idname)

 

2 ) myselect = document.formname.elements["list_2[]"]

 

I am still trying to grasp the basics of javascript, that code is not mine, it's from a tutorial. Could you please show me how I'd modify my existing code to do this? I tend to be more of a visual learner, especially when it comes to coding...

 

Thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.