Page 1 of 1

passing arrays to javascript functions..

Posted: Wed Oct 30, 2002 10:48 am
by waskelton4
Hey all,
I'm having some problems with a webapp i'm working on. My problem comes using a script i found on the web at http://javascript.internet.com/forms/menu-swapper.html

i've taken the code and actually need to use this twice on my page.. however.. i don't think that's what's causing my problem..

What i think my problem is, is that in order for php to take all the values out of a multiple select box(well at least the only way i've done it) is that the select box must be named like an array (i.e. selBox[]) well.. when i prepare the select boxes in the form and rename them and all of the places they are reference in the click events.. the script stops working.. is there any way that any of you know that could get me around this?? maybe having the function accept an array.. i'm kinda lost at this point and any help is greatly appreciated..
my abbreviated code is below..


Thanks.
Will



Javascript function....

Code: Select all

<script language="JavaScript">
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
function move(fbox, tbox) &#123;
var arrFbox = new Array();
var arrTbox = new Array();
var arrLookup = new Array();
var i;
for (i = 0; i < tbox.options.length; i++) &#123;
arrLookup&#1111;tbox.options&#1111;i].text] = tbox.options&#1111;i].value;
arrTbox&#1111;i] = tbox.options&#1111;i].text;
&#125;
var fLength = 0;
var tLength = arrTbox.length;
for(i = 0; i < fbox.options.length; i++) &#123;
arrLookup&#1111;fbox.options&#1111;i].text] = fbox.options&#1111;i].value;
if (fbox.options&#1111;i].selected && fbox.options&#1111;i].value != "") &#123;
arrTbox&#1111;tLength] = fbox.options&#1111;i].text;
tLength++;
&#125;
else &#123;
arrFbox&#1111;fLength] = fbox.options&#1111;i].text;
fLength++;
   &#125;
&#125;
arrFbox.sort();
arrTbox.sort();
fbox.length = 0;
tbox.length = 0;
var c;
for(c = 0; c < arrFbox.length; c++) &#123;
var no = new Option();
no.value = arrLookup&#1111;arrFbox&#1111;c]];
no.text = arrFbox&#1111;c];
fbox&#1111;c] = no;
&#125;
for(c = 0; c < arrTbox.length; c++) &#123;
var no = new Option();
no.value = arrLookup&#1111;arrTbox&#1111;c]];
no.text = arrTbox&#1111;c];
tbox&#1111;c] = no;
   &#125;
&#125;
//  End -->
</script>
php and html form..

Code: Select all

<table width="408" >
<tr> 
<td width="180">
<select name="selBox1" size="5" multiple onDblClick="move(this.form.selBox1,this.form.selBox2&#1111;])">

<?
$SQL = "SELECT * table ORDER BY field";
$result = mysql_query($SQL);
		
     while ($row = mysql_fetch_array($result))
             &#123;?>
                <option value="<?=$row&#1111;0]?>"><?=$row&#1111;1]?></option>
          <? &#125; ?>
</select>
 </td>
 <td>
 <input name="button" type="button" onClick="move(this.form.selBox2&#1111;],this.form.selBox1)" value="<<"> 
              <br>
			   <input name="button" type="button" onClick="move(this.form.selBox1,this.form.selBox2&#1111;])" value=">>"> 
            </td>
            <td width="175"> <select name="selBox2&#1111;]" size="5" multiple id="selBox2&#1111;]" style="width:175" onDblClick="move(this.form.selBox2&#1111;],this.form.selBox1)">
              </select> </td>
          </tr>
        </table>
the page that the data is passed to.. contains..

Code: Select all

echo '<pre>'; 
print_r($HTTP_POST_VARS&#1111;'selBox2']); 
echo '</pre><br>';
still nothing prints out..

Posted: Wed Oct 30, 2002 9:28 pm
by volka
access your elements via getElementById
e.g.

Code: Select all

<html>
<head>
	<script language="JavaScript">
		function alertSelect(which)
		&#123;
			sAlert = "";
			oSelect = document.getElementById(which);
			for(var i=0; i<oSelect.options.length; i++)
			&#123;
				if(oSelect.options&#1111;i].selected && oSelect.options&#1111;i].value != "")
					sAlert += "|"+oSelect.options&#1111;i].value+"|";
			&#125;
			alert(sAlert);
				
		&#125;
	</script>
<body>
	<form>
		<select name="mySelect&#1111;]" id="selectSource" multiple="true" size="5">
			<option value="1">option 1</option>
			<option value="2">option 2</option>
			<option value="3">option 3</option>
			<option value="4">option 4</option>
			<option value="5">option 5</option>
			<option value="6">option 6</option>
		</select>
		<button onClick="alertSelect('selectSource')">click</button>
	</form>
</body>
</html>