<label class="checkbox inline">
<input type="checkbox" name="Services[]" value="service1"> Service 1
</label>
<label class="checkbox inline">
<input type="checkbox" name="Services[]" value="service2"> Service 2
</label>
I am using Services[] so that $_POST[‘Services’] is mapped to an array in PHP, containing the values required.
However, on the client side I also need to uncheck all these checkboxes after the form has been submitted (to PHP). Fairly simple Javascript: (actual checkbox group is 8 long).
for (j=0; j<8; j++){
fields[6][j].checked=false;
}
where:
fields[6] = formob.Services[];
formob=document.forms["FooterQuote"];
The problem is that I cannot put Services[] into a Javascript array. I have tried using the following methods: (pseudo-code, only syntax matters here)
var fields=[formob.text1, formob.text2, formob.text3, formob.Services[]];
//Syntax error at Services[]
var fields=new Array(formob.text1, formob.text2, formob.text3, formob.Services[]);
//Syntax error at Services[]
var fields=new Array(formob.text1, formob.text2, formob.text3);
fields.push(formob.Services[]);
//Syntax error at Services[]
So, unfortunately I can’t push Services[] into a Javascript array, as far as I can tell.
I have also tried using formob.Services instead, and this doesn’t work either.
Has anybody got any ideas how I can make the name=”Services[]” syntax for checkboxes work in a Javascript array, so I can cycle through and uncheck them all? 🙂
You have to address to checkboxes like this:
Have a look at this article for details: Dot and Square Bracket Notations.
So to uncheck it use: