checkboxes php & javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • john

    #1

    checkboxes php & javascript

    hi
    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
    <script>
    function deletecheck()
    {

    for (i=0;i<document .form.elements['projectChecked[]'].length;i++)
    { if(document.for m.elements['projectChecked[]'][i].checked==true)
    {
    return true;
    }
    }
    alert ("Please select a project to delete");
    return false;
    }
    </script>
    <form ......onsubmit= 'return deletecheck();' >
    <?php
    foreach(.....)
    {
    echo "<input type='checkbox' name='projectCh ecked[]'
    value='".$proje ctName."' >";
    }
    ?>
  • Janwillem Borleffs

    #2
    Re: checkboxes php &amp; javascript

    john wrote:[color=blue]
    > 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[/color]

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

    function deletecheck() {
    var pjc = document.form.e lements['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
    validation.


    JW



    Comment

    • john

      #3
      Re: checkboxes php &amp; javascript

      Thanks a lot

      "Janwillem Borleffs" <jw@jwscripts.c om> wrote in message news:<40fed50c$ 0$1746$abc4f4c3 @news.wanadoo.n l>...[color=blue]
      > john wrote:[color=green]
      > > 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[/color]
      >
      > The question is unrelated to php, but since the answer is simple:
      >
      > function deletecheck() {
      > var pjc = document.form.e lements['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
      > validation.
      >
      >
      > JW[/color]

      Comment

      Working...