multiple select option alternative to field_name[]

This is a discussion on multiple select option alternative to field_name[] within the PHP General forums, part of the PHP Programming Forums category; Dear All, The only way I know to retrieve all the values from a multiple select field is to use ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-29-2003
Terence
 
Posts: n/a
Default multiple select option alternative to field_name[]

Dear All,

The only way I know to retrieve all the values from a multiple select field
is to use
the square brackets after the field name ie. user_id[]

The problem however is that when I use a javascript function to transfer the
items
from multiple_select_list1 to multiple_select_list2, I cannot use the square
brackets
in a field name, as this obviously conflicts with something in the script.

So I am wondering if there's any other way in PHP to get the items, or do I
have
to find an alternative way in javascript? If anyone has ideas it would be
great.

Thanks
Terence



my code:

(This works)

echo "<select name='select1' multiple size='7'
class='inputstyle'>....</select>\n";

<input type='button' class='buttonstyle' value=' > ' onClick=\"if
(document.images) copySelected(this.form.select1,this.form.select2)\ ">
<input type='button' class='buttonstyle' value=' < ' onClick=\"if
(document.images) copySelected(this.form.select2,this.form.select1)\ ">
<input type='button' class='buttonstyle' value='>>' onClick=\"if
(document.images) copyAll(this.form.select1,this.form.select2)\">
<input type='button' class='buttonstyle' value='<<' onClick=\"if
(document.images) copyAll(this.form.select2,this.form.select1)\">

<select name='select2' multiple size='7' class='inputstyle'>....</select>

(This is the problem - notice the square brackets behind select2)

echo "<select name='select1' multiple size='7'
class='inputstyle'>....</select>\n";

<input type='button' class='buttonstyle' value=' > ' onClick=\"if
(document.images) copySelected(this.form.select1,this.form.select2[])\">
<input type='button' class='buttonstyle' value=' < ' onClick=\"if
(document.images) copySelected(this.form.select2[],this.form.select1)\">
<input type='button' class='buttonstyle' value='>>' onClick=\"if
(document.images) copyAll(this.form.select1,this.form.select2[])\">
<input type='button' class='buttonstyle' value='<<' onClick=\"if
(document.images) copyAll(this.form.select2[],this.form.select1)\">

<select name='select2[]' multiple size='7' class='inputstyle'>....</select>


javascript:

echo "<script language='JavaScript'><!--\n";
echo "function deleteOption(object,index) {\n";
echo "object.options[index] = null;\n";
echo "}\n";

echo "function addOption(object,text,value) {\n";
echo "var defaultSelected = true;\n";
echo "var selected = true;\n";
echo "var optionName = new Option(text, value, defaultSelected,
selected)\n";
echo "object.options[object.length] = optionName;\n";
echo "}\n";

echo "function copySelected(fromObject,toObject) {\n";
echo "for (var i=0, l=fromObject.options.length;i<l;i++) {\n";
echo "if (fromObject.options[i].selected)\n";
echo
"addOption(toObject,fromObject.options[i].text,fromObject.options[i].value);
\n";
echo "}\n";
echo "for (var i=fromObject.options.length-1;i>-1;i--) {\n";
echo "if (fromObject.options[i].selected)\n";
echo "deleteOption(fromObject,i);\n";
echo "}\n";
echo "}\n";

echo "function copyAll(fromObject,toObject) {\n";
echo "for (var i=0, l=fromObject.options.length;i<l;i++) {\n";
echo
"addOption(toObject,fromObject.options[i].text,fromObject.options[i].value);
\n";
echo "}\n";
echo "for (var i=fromObject.options.length-1;i>-1;i--) {\n";
echo "deleteOption(fromObject,i);\n";
echo "}\n";
echo "}\n";
echo "//--></script>\n";
Reply With Quote
  #2 (permalink)  
Old 12-29-2003
Binay
 
Posts: n/a
Default Re: [PHP] multiple select option alternative to field_name[]

Hi Terence

Assign ids to multiple select field and then use id attribute of select tag
in JavaScript to access (copy,del etc.) instead of using the name attribute.

Hope this helps.

Cheers
Binay
----- Original Message -----
From: "Terence" <terence@sunway.edu.my>
To: <php-general@lists.php.net>
Sent: Monday, December 29, 2003 11:46 AM
Subject: [php] multiple select option alternative to field_name[]


> Dear All,
>
> The only way I know to retrieve all the values from a multiple select

field
> is to use
> the square brackets after the field name ie. user_id[]
>
> The problem however is that when I use a javascript function to transfer

the
> items
> from multiple_select_list1 to multiple_select_list2, I cannot use the

square
> brackets
> in a field name, as this obviously conflicts with something in the script.
>
> So I am wondering if there's any other way in PHP to get the items, or do

I
> have
> to find an alternative way in javascript? If anyone has ideas it would be
> great.
>
> Thanks
> Terence
>
>
>
> my code:
>
> (This works)
>
> echo "<select name='select1' multiple size='7'
> class='inputstyle'>....</select>\n";
>
> <input type='button' class='buttonstyle' value=' > ' onClick=\"if
> (document.images) copySelected(this.form.select1,this.form.select2)\ ">
> <input type='button' class='buttonstyle' value=' < ' onClick=\"if
> (document.images) copySelected(this.form.select2,this.form.select1)\ ">
> <input type='button' class='buttonstyle' value='>>' onClick=\"if
> (document.images) copyAll(this.form.select1,this.form.select2)\">
> <input type='button' class='buttonstyle' value='<<' onClick=\"if
> (document.images) copyAll(this.form.select2,this.form.select1)\">
>
> <select name='select2' multiple size='7' class='inputstyle'>....</select>
>
> (This is the problem - notice the square brackets behind select2)
>
> echo "<select name='select1' multiple size='7'
> class='inputstyle'>....</select>\n";
>
> <input type='button' class='buttonstyle' value=' > ' onClick=\"if
> (document.images) copySelected(this.form.select1,this.form.select2[])\">
> <input type='button' class='buttonstyle' value=' < ' onClick=\"if
> (document.images) copySelected(this.form.select2[],this.form.select1)\">
> <input type='button' class='buttonstyle' value='>>' onClick=\"if
> (document.images) copyAll(this.form.select1,this.form.select2[])\">
> <input type='button' class='buttonstyle' value='<<' onClick=\"if
> (document.images) copyAll(this.form.select2[],this.form.select1)\">
>
> <select name='select2[]' multiple size='7'

class='inputstyle'>....</select>
>
>
> javascript:
>
> echo "<script language='JavaScript'><!--\n";
> echo "function deleteOption(object,index) {\n";
> echo "object.options[index] = null;\n";
> echo "}\n";
>
> echo "function addOption(object,text,value) {\n";
> echo "var defaultSelected = true;\n";
> echo "var selected = true;\n";
> echo "var optionName = new Option(text, value, defaultSelected,
> selected)\n";
> echo "object.options[object.length] = optionName;\n";
> echo "}\n";
>
> echo "function copySelected(fromObject,toObject) {\n";
> echo "for (var i=0, l=fromObject.options.length;i<l;i++) {\n";
> echo "if (fromObject.options[i].selected)\n";
> echo
>

"addOption(toObject,fromObject.options[i].text,fromObject.options[i].value);
> \n";
> echo "}\n";
> echo "for (var i=fromObject.options.length-1;i>-1;i--) {\n";
> echo "if (fromObject.options[i].selected)\n";
> echo "deleteOption(fromObject,i);\n";
> echo "}\n";
> echo "}\n";
>
> echo "function copyAll(fromObject,toObject) {\n";
> echo "for (var i=0, l=fromObject.options.length;i<l;i++) {\n";
> echo
>

"addOption(toObject,fromObject.options[i].text,fromObject.options[i].value);
> \n";
> echo "}\n";
> echo "for (var i=fromObject.options.length-1;i>-1;i--) {\n";
> echo "deleteOption(fromObject,i);\n";
> echo "}\n";
> echo "}\n";
> echo "//--></script>\n";
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php

Reply With Quote
  #3 (permalink)  
Old 12-29-2003
Marek Kilimajer
 
Posts: n/a
Default Re: [PHP] multiple select option alternative to field_name[]

Use this.form.elements['select1[]']

Terence wrote:

> Dear All,
>
> The only way I know to retrieve all the values from a multiple select field
> is to use
> the square brackets after the field name ie. user_id[]
>
> The problem however is that when I use a javascript function to transfer the
> items
> from multiple_select_list1 to multiple_select_list2, I cannot use the square
> brackets
> in a field name, as this obviously conflicts with something in the script.
>
> So I am wondering if there's any other way in PHP to get the items, or do I
> have
> to find an alternative way in javascript? If anyone has ideas it would be
> great.
>
> Thanks
> Terence
>
>
>
> my code:
>
> (This works)
>
> echo "<select name='select1' multiple size='7'
> class='inputstyle'>....</select>\n";
>
> <input type='button' class='buttonstyle' value=' > ' onClick=\"if
> (document.images) copySelected(this.form.select1,this.form.select2)\ ">
> <input type='button' class='buttonstyle' value=' < ' onClick=\"if
> (document.images) copySelected(this.form.select2,this.form.select1)\ ">
> <input type='button' class='buttonstyle' value='>>' onClick=\"if
> (document.images) copyAll(this.form.select1,this.form.select2)\">
> <input type='button' class='buttonstyle' value='<<' onClick=\"if
> (document.images) copyAll(this.form.select2,this.form.select1)\">
>
> <select name='select2' multiple size='7' class='inputstyle'>....</select>
>
> (This is the problem - notice the square brackets behind select2)
>
> echo "<select name='select1' multiple size='7'
> class='inputstyle'>....</select>\n";
>
> <input type='button' class='buttonstyle' value=' > ' onClick=\"if
> (document.images) copySelected(this.form.select1,this.form.select2[])\">
> <input type='button' class='buttonstyle' value=' < ' onClick=\"if
> (document.images) copySelected(this.form.select2[],this.form.select1)\">
> <input type='button' class='buttonstyle' value='>>' onClick=\"if
> (document.images) copyAll(this.form.select1,this.form.select2[])\">
> <input type='button' class='buttonstyle' value='<<' onClick=\"if
> (document.images) copyAll(this.form.select2[],this.form.select1)\">
>
> <select name='select2[]' multiple size='7' class='inputstyle'>....</select>
>
>
> javascript:
>
> echo "<script language='JavaScript'><!--\n";
> echo "function deleteOption(object,index) {\n";
> echo "object.options[index] = null;\n";
> echo "}\n";
>
> echo "function addOption(object,text,value) {\n";
> echo "var defaultSelected = true;\n";
> echo "var selected = true;\n";
> echo "var optionName = new Option(text, value, defaultSelected,
> selected)\n";
> echo "object.options[object.length] = optionName;\n";
> echo "}\n";
>
> echo "function copySelected(fromObject,toObject) {\n";
> echo "for (var i=0, l=fromObject.options.length;i<l;i++) {\n";
> echo "if (fromObject.options[i].selected)\n";
> echo
> "addOption(toObject,fromObject.options[i].text,fromObject.options[i].value);
> \n";
> echo "}\n";
> echo "for (var i=fromObject.options.length-1;i>-1;i--) {\n";
> echo "if (fromObject.options[i].selected)\n";
> echo "deleteOption(fromObject,i);\n";
> echo "}\n";
> echo "}\n";
>
> echo "function copyAll(fromObject,toObject) {\n";
> echo "for (var i=0, l=fromObject.options.length;i<l;i++) {\n";
> echo
> "addOption(toObject,fromObject.options[i].text,fromObject.options[i].value);
> \n";
> echo "}\n";
> echo "for (var i=fromObject.options.length-1;i>-1;i--) {\n";
> echo "deleteOption(fromObject,i);\n";
> echo "}\n";
> echo "}\n";
> echo "//--></script>\n";
>

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:29 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0