checkboxes php & javascript

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I am generating a set of dynamic checkboxes via php.
I have to check that the uer should atleast check one checkbox. When
there are more then 1 checkbox my javascript works but if there is
only one check box then my javascript doesnt works. My code is
function deletecheck()

for (i=0;i<document.form.elements['projectChecked[]'].length;i++)
{ if(document.form.elements['projectChecked[]'][i].checked==true)
return true;
alert ("Please select a project to delete");
return false;
<form ......onsubmit='return deletecheck();'>
echo "<input type='checkbox' name='projectChecked[]'
value='".$projectName."' >";

Re: checkboxes php & javascript

john wrote:
Quoted text here. Click to load it

The question is unrelated to php, but since the answer is simple:

function deletecheck() {
    var pjc = document.form.elements['projectChecked[]'];
    var returnVal = false;

    if (!pjc.length) {
        returnVal = pjc.checked;
    } else {
        for (i=0; i<pjc.length && !returnVal; i++) {
            returnVal = pjc[i].checked;
    if (!returnVal) alert ("Please select a project to delete");
    return returnVal;

BTW, never rely on JavaScript. Always back it up with server-side


Re: checkboxes php & javascript

Thanks a lot

Quoted text here. Click to load it

Site Timeline