title: Check all checkbox and store all selected items in a global variable
body: <p>I have a list of emails with a checkbox next to it, the user will be able to select which address he/she wants to email. Now i've added another checkbox that when checked will check all the other checkbox. Below is the code i wrote (with help from stackoverflow of course) :</p>
  
  <pre><code>&lt;SCRIPT LANGUAGE="JavaScript"&gt;
  
  function selectFunction (checkall,field)
  {
      if(checkall.checked==true){
          for (i = 0; i &lt; field.length; i++)
              field[i].checked = true ;
      }else{
          for (i = 0; i &lt; field.length; i++)
              field[i].checked = false ;
      }
  }
  
  &lt;/script&gt;
  
  &lt;form name="myform" action="profile-invite.html" method="post"&gt;
      &lt;b&gt;Your Favorite Scripts &amp; Languages&lt;/b&gt;&lt;br&gt;
      &lt;input type="checkbox" name="list" value="1"&gt;aaa@xxx.com&lt;br&gt;
      &lt;input type="checkbox" name="list" value="2"&gt;bbb@xxx.com&lt;br&gt;
      &lt;input type="checkbox" name="list" value="3"&gt;ccc@xxx.com&lt;br&gt;
      &lt;input type="checkbox" name="list" value="4"&gt;ddd@xxx.com&lt;br&gt;
      &lt;input type="checkbox" name="list" value="5"&gt;eee@xxx.com&lt;br&gt;
  
      &lt;input type="checkbox" name="selectallcb" value="Check All"
      onClick="selectFunction(document.myform.selectallcb,document.myform.list)"&gt;
      &lt;input type="submit" name="formSubmit" value="Submit" /&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p>The "select all" function works fine, but using print_r i saw that when the submit button is clicked, the submited value is the last checkbox that i selected. For example if i click 5-3-1-2, the value in $_POST is "2" and not the rest.</p>
  
  <p>i realized that my code can only register ONE selected checkbox, therefore only the last one is taken into account. So I rewrote the code by adding [] behind the name of the checkbox : </p>
  
  <pre><code>&lt;form name="myform" action="profile-invite.html" method="post"&gt;
      &lt;b&gt;Your Favorite Scripts &amp; Languages&lt;/b&gt;&lt;br&gt;
      &lt;input type="checkbox" name="list[]" value="1"&gt;aaa@xxx.com&lt;br&gt;
      &lt;input type="checkbox" name="list[]" value="2"&gt;bbb@xxx.com&lt;br&gt;
      &lt;input type="checkbox" name="list[]" value="3"&gt;ccc@xxx.com&lt;br&gt;
      &lt;input type="checkbox" name="list[]" value="4"&gt;ddd@xxx.com&lt;br&gt;
      &lt;input type="checkbox" name="list[]" value="5"&gt;eee@xxx.com&lt;br&gt;
  
      &lt;input type="checkbox" name="selectallcb" value="Check All"
      onClick="selectFunction(document.myform.selectallcb,document.myform.list)"&gt;
      &lt;input type="submit" name="formSubmit" value="Submit" /&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p>Now, it registers the multiple selections when i checked with print_r.(if i click 5-3-1-2, the value in $_POST is now [0]=>5,[1]=>3,[2]=>1,[3]=>2.)</p>
  
  <p>But the "select all" checkbox doesn't work anymore. I assume is due to the [] which transformed the field into an array. I tried various methods (by replacing "document.myform.list" with "document.myform.list[]" etc. ) None is working so far, i'll continue experimenting but if anybody have a clear idea on how to merge the 2 codes above please help.</p>
  
  <p>Thank You</p>