Image

Imagef_it wrote in Imagejavascript

Variable name dilemma

My problem is that I am trying to create a checkbox name something along the lines of 'whatever[]'. The reason it looks like an array is because the script is working with form elements (the checkboxes) that I need PHP to put into an array after submitting the form.

Because I have the two brackets ([]) in the checkbox name, my javascript doesn't want to work anymore.

var checkflag = "false";

function check(field) {
   if (checkflag == "false") {
      for (i = 0; i < field.length; i++) {
         field[i].checked = true;
      }
      enable();
      checkflag = "true";
      return "Uncheck All";
   } else {
      for (i = 0; i < field.length; i++) {
         field[i].checked = false;
      }
      enable();
      checkflag = "false";
      return "Check All";
   }
}

var ch = "0";

function enable() {
   for (i = 0; i < document.delform.destroy.length; i++) {
      if (document.delform.destroy[i].checked == true) {
         ch++;
      }
   }
   if (ch > 0) {
      document.delform.sbutton.disabled = false;
      ch = "0";
   } else {
      document.delform.sbutton.disabled = true;
   }
}


And it goes with this bit of my form:

            <td style="border:1px solid #000000; text-align:center;" valign="middle">
              <input type="checkbox" name="destroy" value="whatever" onClick="javascript:enable();" />
            </td>
          </tr>
        </table>
        <p style="text-align:right;">
          <input type="button" value=" Check All " onClick="javascript:this.value=check(this.form.destroy)" />
        </p>
        <p class="center">
          <input type="submit" name="sbutton" value=" Submit " onClick="javascript:delconfirm();" disabled />
        </p>


I need the var name of the checkboxes to be 'destroy[]' (instead of just 'destroy') and be able to work with the two javascript snippets above.

I'm pretty sure theres nothing thats going to make it will work because of the form element arrays in the javascript conflicting with the brackets in the var name, but if anyone has any tips or suggestions I'd greatly appreciate it.