multiple checkboxes help needed

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

    multiple checkboxes help needed

    I have multiple checkbox's created with an array name because I have
    many on the same web page - their names are like:

    frm_chk_delete[0]
    frm_chk_delete[1]
    frm_chk_delete[2]
    frm_chk_delete[3]
    etc.

    Here is my code line that creates each checkbox (the php variable get
    incremented of course):

    <INPUT TYPE="checkbox" NAME="frm_chk_d elete[<?php echo
    $php_delete_cou nter;?>]">Delete

    When I click on the Delete button I want to call a JS routine to check
    that at least one checknbox has been checked and marked for deletion
    when the form submit is called my php file will detect and delete the
    checkboxes accordingly.

    I'd like to use JS before the submit and make sure at least one of the
    checkboxes is checked - how do I loop through these array name
    checkboxes in javascript to check their states before submitting?

    Thanks...

  • Lasse Reichstein Nielsen

    #2
    Re: multiple checkboxes help needed

    Ralph Freshour <ralph@primemai l.com> writes:
    [color=blue]
    > frm_chk_delete[0]
    > frm_chk_delete[1]
    > frm_chk_delete[2]
    > frm_chk_delete[3][/color]
    ....[color=blue]
    > <INPUT TYPE="checkbox" NAME="frm_chk_d elete[<?php echo
    > $php_delete_cou nter;?>]">Delete[/color]
    ....[color=blue]
    > I'd like to use JS before the submit and make sure at least one of the
    > checkboxes is checked - how do I loop through these array name
    > checkboxes in javascript to check their states before submitting?[/color]

    ---
    <script type="text/javascript">
    function isOneSet(form) {
    for (var i=0;i<numberOfC heckboxes;i++) {
    if (form.elements["frm_chk_de lete["+i+"]"].checked) {
    return true;
    }
    }
    return false;
    }
    </script>
    ---
    and
    ---
    <form ... onsubmit="if (!isOneSet(this )) {alert('SET ONE!');return false;}">
    ---

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Ralph Freshour

      #3
      Re: multiple checkboxes help needed

      How do I set the "numberOfCheckb oxes"?

      To test the code I replaced "numberOfCheckb oxes" with 2 and that
      worked but then it choked on the if statement.

      I'm using win98 and IE 6.0

      Thanks...

      On Sun, 14 Dec 2003 17:27:26 +0100, Lasse Reichstein Nielsen
      <lrn@hotpop.com > wrote:
      [color=blue]
      >Ralph Freshour <ralph@primemai l.com> writes:
      >[color=green]
      >> frm_chk_delete[0]
      >> frm_chk_delete[1]
      >> frm_chk_delete[2]
      >> frm_chk_delete[3][/color]
      >...[color=green]
      >> <INPUT TYPE="checkbox" NAME="frm_chk_d elete[<?php echo
      >> $php_delete_cou nter;?>]">Delete[/color]
      >...[color=green]
      >> I'd like to use JS before the submit and make sure at least one of the
      >> checkboxes is checked - how do I loop through these array name
      >> checkboxes in javascript to check their states before submitting?[/color]
      >
      >---
      ><script type="text/javascript">
      >function isOneSet(form) {
      > for (var i=0;i<numberOfC heckboxes;i++) {
      > if (form.elements["frm_chk_de lete["+i+"]"].checked) {
      > return true;
      > }
      > }
      > return false;
      >}
      ></script>
      >---
      >and
      >---
      ><form ... onsubmit="if (!isOneSet(this )) {alert('SET ONE!');return false;}">
      >---
      >
      >/L[/color]

      Comment

      • Michael Winter

        #4
        Re: multiple checkboxes help needed

        Ralph Freshour wrote on 14 Dec 2003 at Sun, 14 Dec 2003 16:00:11
        GMT:
        [color=blue]
        > I have multiple checkbox's created with an array name because I
        > have many on the same web page - their names are like:
        >
        > frm_chk_delete[0]
        > frm_chk_delete[1]
        > frm_chk_delete[2]
        > frm_chk_delete[3]
        > etc.
        >
        > Here is my code line that creates each checkbox (the php
        > variable get incremented of course):
        >
        > <INPUT TYPE="checkbox" NAME="frm_chk_d elete[<?php echo
        > $php_delete_cou nter;?>]">Delete
        >
        > When I click on the Delete button I want to call a JS routine to
        > check that at least one checknbox has been checked and marked
        > for deletion when the form submit is called my php file will
        > detect and delete the checkboxes accordingly.
        >
        > I'd like to use JS before the submit and make sure at least one
        > of the checkboxes is checked - how do I loop through these array
        > name checkboxes in javascript to check their states before
        > submitting?[/color]

        It would be easier if they all had the same name, but it can be done
        as it is.

        <FORM ... onsubmit="retur n checkBoxes(this )">

        function checkBoxes(form ) {
        var numControls = form.elements.l ength;
        var checks = 0;

        // Check every control in the form
        for (var i = 0; i < numControls; ++i) {
        var element = form.elements[i];

        // If the name begins, frm_chk_delete[ ...
        if ('frm_chk_delet e[' == element.name.su bstr(0, 15)) {
        // ...see if it's checked, and increment counter if it is
        if (element.checke d) checks++;
        }
        }
        // If none of boxes have been checked, alert the user and cancel
        // the submission
        if (!checks) {
        window.alert('P lease check at least one box before submitting');
        return false;
        }

        // If code reaches here, at least one box was checked, so allow
        // the form to submit
        return true;
        }

        If it's not guaranteed that only checkboxes will have names that
        start with 'frm_chk_delete[', you should also check to make sure that
        the element being inspected is a checkbox (use element.type ==
        'checkbox').

        Hope that helps,

        Mike

        --
        Michael Winter
        M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk")

        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: multiple checkboxes help needed

          Ralph Freshour <ralph@primemai l.com> writes:
          [color=blue]
          > How do I set the "numberOfCheckb oxes"?[/color]

          I don't know. It is the number of checkboxes you have created, so I'll
          assume you insert it with PHP somehow.
          [color=blue]
          > To test the code I replaced "numberOfCheckb oxes" with 2 and that
          > worked but then it choked on the if statement.[/color]

          "choked"? Not a useful error report :)
          You'll have to be a little more informative, or we won't be able
          to help. What is the *exact* error message? Can we see the page?

          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • Ralph Freshour

            #6
            Re: multiple checkboxes help needed

            Sorry - the error is in the lower left corner of my browser: "Error on
            page"

            That's all it says - the code below displays 1 and 2 and give the
            error msg - so it must be in the if statement:

            function atLeastOneCheck ed(form){
            alert("1");
            for (var i=0;i<2;i++) {
            alert("2");
            if (form.elements["frm_chk_de lete["+i+"]"].checked) {
            alert("3");
            return true;
            }
            }
            alert("Please check at least one box!");
            return false;
            }




            On Sun, 14 Dec 2003 19:00:45 +0100, Lasse Reichstein Nielsen
            <lrn@hotpop.com > wrote:
            [color=blue]
            >Ralph Freshour <ralph@primemai l.com> writes:
            >[color=green]
            >> How do I set the "numberOfCheckb oxes"?[/color]
            >
            >I don't know. It is the number of checkboxes you have created, so I'll
            >assume you insert it with PHP somehow.
            >[color=green]
            >> To test the code I replaced "numberOfCheckb oxes" with 2 and that
            >> worked but then it choked on the if statement.[/color]
            >
            >"choked"? Not a useful error report :)
            >You'll have to be a little more informative, or we won't be able
            >to help. What is the *exact* error message? Can we see the page?
            >
            >/L[/color]

            Comment

            • Ralph Freshour

              #7
              Re: multiple checkboxes help needed

              this line is giving me "Error on page" msg in lower left corner of my
              browser:

              var numControls = form.elements.l ength;

              ???



              On Sun, 14 Dec 2003 17:55:13 GMT, Michael Winter
              <M.Winter@bluey onder.co.invali d> wrote:
              [color=blue]
              >Ralph Freshour wrote on 14 Dec 2003 at Sun, 14 Dec 2003 16:00:11
              >GMT:
              >[color=green]
              >> I have multiple checkbox's created with an array name because I
              >> have many on the same web page - their names are like:
              >>
              >> frm_chk_delete[0]
              >> frm_chk_delete[1]
              >> frm_chk_delete[2]
              >> frm_chk_delete[3]
              >> etc.
              >>
              >> Here is my code line that creates each checkbox (the php
              >> variable get incremented of course):
              >>
              >> <INPUT TYPE="checkbox" NAME="frm_chk_d elete[<?php echo
              >> $php_delete_cou nter;?>]">Delete
              >>
              >> When I click on the Delete button I want to call a JS routine to
              >> check that at least one checknbox has been checked and marked
              >> for deletion when the form submit is called my php file will
              >> detect and delete the checkboxes accordingly.
              >>
              >> I'd like to use JS before the submit and make sure at least one
              >> of the checkboxes is checked - how do I loop through these array
              >> name checkboxes in javascript to check their states before
              >> submitting?[/color]
              >
              >It would be easier if they all had the same name, but it can be done
              >as it is.
              >
              ><FORM ... onsubmit="retur n checkBoxes(this )">
              >
              >function checkBoxes(form ) {
              > var numControls = form.elements.l ength;
              > var checks = 0;
              >
              > // Check every control in the form
              > for (var i = 0; i < numControls; ++i) {
              > var element = form.elements[i];
              >
              > // If the name begins, frm_chk_delete[ ...
              > if ('frm_chk_delet e[' == element.name.su bstr(0, 15)) {
              > // ...see if it's checked, and increment counter if it is
              > if (element.checke d) checks++;
              > }
              > }
              > // If none of boxes have been checked, alert the user and cancel
              > // the submission
              > if (!checks) {
              > window.alert('P lease check at least one box before submitting');
              > return false;
              > }
              >
              > // If code reaches here, at least one box was checked, so allow
              > // the form to submit
              > return true;
              >}
              >
              >If it's not guaranteed that only checkboxes will have names that
              >start with 'frm_chk_delete[', you should also check to make sure that
              >the element being inspected is a checkbox (use element.type ==
              >'checkbox').
              >
              >Hope that helps,
              >
              >Mike[/color]

              Comment

              • Lasse Reichstein Nielsen

                #8
                Re: multiple checkboxes help needed

                Ralph Freshour <ralph@primemai l.com> writes:
                [color=blue]
                > Sorry - the error is in the lower left corner of my browser: "Error on
                > page"[/color]

                Your browser is some version of Internet Explorer, correct?
                You can click on the error icon and get a better explanation (not good,
                just better).
                You can turn error messages on in:
                Tools > Internet Options > Advanced > Browsing :
                Display a notification about every scrip error.
                [color=blue]
                > That's all it says - the code below displays 1 and 2 and give the
                > error msg - so it must be in the if statement:[/color]
                [color=blue]
                > if (form.elements["frm_chk_de lete["+i+"]"].checked) {[/color]

                If this fails, it is because something doesn't have the correct value.

                Either "form" is not pointing to the correct form, or it has no
                element called "frm_chk_de lete[0]". Try adding alerts before the if:
                alert(form);
                alert(form.elem ents["frm_chk_de lete[0]"]);

                Check that the form is written correctly.

                And please don't top post! At least trim your quotes.

                /L
                --
                Lasse Reichstein Nielsen - lrn@hotpop.com
                DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                'Faith without judgement merely degrades the spirit divine.'

                Comment

                • Ralph Freshour

                  #9
                  Re: multiple checkboxes help needed

                  On Sun, 14 Dec 2003 21:46:52 +0100, Lasse Reichstein Nielsen
                  <lrn@hotpop.com > wrote:
                  [color=blue]
                  >Ralph Freshour <ralph@primemai l.com> writes:
                  >[color=green]
                  >> Sorry - the error is in the lower left corner of my browser: "Error on
                  >> page"[/color]
                  >
                  >Your browser is some version of Internet Explorer, correct?
                  >You can click on the error icon and get a better explanation (not good,
                  >just better).
                  >You can turn error messages on in:
                  > Tools > Internet Options > Advanced > Browsing :
                  > Display a notification about every scrip error.
                  >[color=green]
                  >> That's all it says - the code below displays 1 and 2 and give the
                  >> error msg - so it must be in the if statement:[/color]
                  >[color=green]
                  >> if (form.elements["frm_chk_de lete["+i+"]"].checked) {[/color]
                  >
                  >If this fails, it is because something doesn't have the correct value.
                  >
                  >Either "form" is not pointing to the correct form, or it has no
                  >element called "frm_chk_de lete[0]". Try adding alerts before the if:
                  > alert(form);
                  > alert(form.elem ents["frm_chk_de lete[0]"]);
                  >
                  >Check that the form is written correctly.
                  >
                  >And please don't top post! At least trim your quotes.
                  >
                  >/L[/color]


                  Thanks for the info about turning on the details of the err msg in IE
                  - I saw the problem was in

                  var numControls = form.elements.l ength;

                  so in my calling statement I changed it from (this) to (this.form) and
                  then it worked ok.

                  Thanks for help everyone...


                  Comment

                  Working...