[Javascript] submitting java script array using Form

Reuben D. Budiardja reubendb at innovativethought.com
Tue Mar 11 12:49:29 CST 2003


Hello,
I am wondering if there's an easy way to do this. Let me describe the problem.

I have 2 forms, in form 1, I have a set of checkboxes with the same name but 
each with different value. The checkboxes were generated using PHP on server 
side. I want it when the user check the checkbox, it value goes to an array, 
or something. 

Then the submit button is on Form2. Form2 only contains the submit button and 
nothing else visible (it can have INPUT TYPE=HIDDEN of course).

When user click the submit button, I want all the values that are checked on 
the checkbox to be submitted.

So the very short version of the problem, how do I transfer those values from 
Form1 to Form2 ? Via javascript variable?
Here what I am thinking:

<FORM NAME="build">
  <INPUT TYPE="CHEKBOX" NAME="list[]" VALUE="value 1">
  <INPUT TYPE="CHEKBOX" NAME="list[]" VALUE="value 1">
  <INPUT TYPE="CHEKBOX" NAME="list[]" VALUE="value 1">
</FORM>

<FORM NAME="send">
  <INPUT TYPE=HIDDEN NAME="list[]">
  <INPUT TYPE="BUTTON" onClick="sendList();">
</FORM>

<SCRIPT>
// Pseudocode -- does not work
function sendList()
  {
  // Try to access the value checkbox in form 1 and assign it to form 2
  var checbox_val = document.getElementsByName["build"].list;
  document.getElementsByName["send"].list = checkbox_val;
  document.getElementsByName["send"].submit();
  }
</SCRIPT>

I am trying to do it using the DOM way (I am not going to worry about older 
browser, primary target audience use Netscape 7 / Mozilla > 1.0)
But I am all confused on how to do this.

The reason I use [] for the checkbox name is because PHP can interpret that 
directly as an array of value. But even if that can't be done, I'll be happy 
with comma separated values as I can explode it easily into array in PHP.

Any help on this will be greatly appreciated. Thanks

Further more, is there any good tutorial on how to use javascript to access 
the DOM elements? I used to do it the old way (like document.forms[0]. etc) 
but I am not sure if that the recomended way anymore.

Thanks.

RDB





More information about the Javascript mailing list