PHP-Javascript method for auto-selecting checkbox groups

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

    #1

    PHP-Javascript method for auto-selecting checkbox groups

    I am producing a form using PHP on the back end and Javascript on the
    front end. The resulting script will come to the browser as follows:

    Code:
    <script>
    <!--
    function selectAll() {
    moveElement = eval("document.imageForm.move" + "[]");
    alert(moveElement);
    if (document.imageForm.select_all[0].checked) {
    for (i = 0; i < document.imageForm.move.length; i++) {
    document.imageForm.move[i].checked = true;
    }
    }
    }
    
    function deSelectAll() {
    if (!document.imageForm.select_all[0].checked ||
    document.imageForm.deselect_all[0].checked) {
    for (i = 0; i < document.imageForm.move.length; i++) {
    document.imageForm.move[i].checked = false;
    }
    }
    }
    //-->
    </script>
    This will work with a checkbox group that has to have this naming
    convention for PHP:

    Code:
    <input type=checkbox name=move[] value="myImage.gif"> - Move?
    I will also have a checkbox that will "select all" and "de-select
    all":

    Code:
    <input type=checkbox name=select_all value=1 onChange="selectAll()"> -
    Select All&nbsp;&nbsp;
    <input type=checkbox name=deselect_all value=1
    onChange="deSelectAll()"> - De-select All
    I am not familiar at how Javascript can work with non-standard naming
    conventions for event actions, can someone tell me what I'm doing
    wrong? This fails at least in Mozilla 1.6

    Thanx
    Phil
  • Matt Kruse

    #2
    Re: PHP-Javascript method for auto-selecting checkbox groups

    "Phil Powell" <soazine@erols. com> wrote:[color=blue]
    > This will work with a checkbox group that has to have this naming
    > convention for PHP:
    > I will also have a checkbox that will "select all" and "de-select
    > all":[/color]

    Checkboxes don't have onChange() events, they have onclick() events.

    If you want to ease your pain a bit, you could consider using my "Checkbox
    Group" library, which would handle all this for you quite easily:


    --
    Matt Kruse
    Javascript Toolbox: http://www.mattkruse.com/javascript/


    Comment

    • The Nordic One

      #3
      Re: PHP-Javascript method for auto-selecting checkbox groups

      I'm sorry but please provide a very simple tutorial on this, you went so
      far over my head that you were on Jupiter and I'm underground.

      Phil

      ---------------------------------------
      TCL and PHP: The Superior Web Languages!
      Javascript: Is Cool
      ASP: why?


      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Reply Via Newsgroup

        #4
        Re: PHP-Javascript method for auto-selecting checkbox groups

        Phil Powell wrote:
        [color=blue]
        > I am producing a form using PHP on the back end and Javascript on the
        > front end. The resulting script will come to the browser as follows:
        >
        >
        Code:
        > <script>
        > <!--
        >   function selectAll() {
        >    moveElement = eval("document.imageForm.move" + "[]");
        >    alert(moveElement);
        >    if (document.imageForm.select_all[0].checked) {
        >     for (i = 0; i < document.imageForm.move.length; i++) {
        >      document.imageForm.move[i].checked = true;
        >     }
        >   }
        >  }
        >
        >  function deSelectAll() {
        >   if (!document.imageForm.select_all[0].checked ||
        > document.imageForm.deselect_all[0].checked) {
        >    for (i = 0; i < document.imageForm.move.length; i++) {
        >     document.imageForm.move[i].checked = false;
        >    }
        >   }
        >  }
        > //-->
        > </script>
        >
        >
        > This will work with a checkbox group that has to have this naming
        > convention for PHP:
        >
        >
        Code:
        > <input type=checkbox name=move[] value="myImage.gif"> - Move?
        >
        >
        > I will also have a checkbox that will "select all" and "de-select
        > all":
        >
        >
        Code:
        > <input type=checkbox name=select_all value=1 onChange="selectAll()"> -
        > Select All&nbsp;&nbsp;
        > <input type=checkbox name=deselect_all value=1
        > onChange="deSelectAll()"> - De-select All
        >
        >
        > I am not familiar at how Javascript can work with non-standard naming
        > conventions for event actions, can someone tell me what I'm doing
        > wrong? This fails at least in Mozilla 1.6
        >
        > Thanx
        > Phil[/color]


        Your problem is reasonably common (search google groups for select box
        and php and I'm sure you'll find a few recent posts (one or two from me)
        related to this.

        However... there are two methods that you can use to refer to an object
        in a form in javascript - I'm a newbie (six months) but think I'm
        getting pretty strong... someone though might have some other
        method/solution... however, use something like

        boxLength=docum ent.forms["formGroups "].elements["userSelect ed[]"].length;

        where by 'formGroups' is the name of your form and 'userSelect[]' is the
        name of your select box.

        Using my above example, boxLength would equal the total number of
        selections in a multi-select box.

        Also... put quotes around your INPUT TAG name field... thus

        Use[color=blue]
        > <input type=checkbox name="move[]" value="myImage. gif">[/color]
        not[color=blue]
        > <input type=checkbox name=move[] value="myImage. gif">[/color]

        as the [] might cause some grief on some web browsers...

        Lastly - I just learned that a mis-use of eval is common - there are
        other methods that you can use that use less resources and faster (for
        example, from a numeric point of view you could use something like
        parseInt() - Do a search on google groups for a recent message I got
        which explains the reasons - search for "why is eval evil" in
        comp.lang.javas cript for a detailed explanation.

        Hope something above gives you some help...

        Regards
        Randell D.

        Comment

        • Phil Powell

          #5
          Re: PHP-Javascript method for auto-selecting checkbox groups

          Following occurred when my colleague tested on his machine using IE
          6.0 SP 4 for Win2000 (I am using Linux/Mozilla):

          Ok, back to the drawing board, it didn't work when my colleague tested
          on his machine using IE 6.0 SP4 for Win2K:

          I tried with the HTML code below, but IE spat out the following:

          Line: 42
          Char: 1
          Error: 'document.image Form.move[]' is null or not an object
          Code: 0
          URL: file://C:\Documents and Settings\eduard o\Desktop\test. html

          HTML code:

          <html>
          <head>
          <title></title>
          <body>
          <script>
          <!--
          ~ function selectAll(check State, selectStatus, obj) {
          ~ if (document.image Form.select_all .checked ||
          document.imageF orm.deselect_al l.checked) {
          ~ for (i = 0; i < obj.length; i++) {
          ~ obj[i].checked = checkState;
          ~ }
          ~ if (selectStatus.t oLowerCase() == 'select')
          document.imageF orm.deselect_al l.checked = false;
          ~ if (selectStatus.t oLowerCase() == 'deselect')
          document.imageF orm.select_all. checked = false;
          ~ }
          }
          //-->
          </script>

          <table>
          <tr>
          ~ <td bgcolor=ffffff> <a
          href="./images/mu-spin/kyrka.jpg">kyrk a.jpg</a></td>
          ~ <td bgcolor=ffffff> Show Metadata</td>
          ~ <td bgcolor=ffffff> <input type=checkbox name="move[]"
          value="kyrka.jp g"> - Move?</td>

          </tr>
          <tr>
          ~ <td bgcolor=ffffcc> <a
          href="./images/mu-spin/banner.jpg.jpeg ">banner.jpg.jp eg</a></td>
          ~ <td bgcolor=ffffcc> <a
          href=index.php? section=image&a ction=edit&choo seAlbum=1&album =mu-spin&id=147>Sho w
          Metadata</a></td>
          ~ <td bgcolor=ffffcc> <input type=checkbox name="move[]"
          value="banner.j pg.jpeg"> - Move?</td>
          </tr>
          <tr>

          ~ <td bgcolor=ffffff> <a
          href="./images/mu-spin/kyrka_gray.jpg" >kyrka_gray.jpg </a></td>
          ~ <td bgcolor=ffffff> <a
          href=index.php? section=image&a ction=edit&choo seAlbum=1&album =mu-spin&id=145>Sho w
          Metadata</a></td>
          ~ <td bgcolor=ffffff> <input type=checkbox name="move[]"
          value="kyrka_gr ay.jpg"> - Move?</td>
          </tr>
          <tr>
          ~ <td colspan=3 bgcolor=ddddee>
          ~ <input type=checkbox name="select_al l" value=1
          onClick="select All(true, 'select', document.imageF orm['move[]'])"> -
          Select All&nbsp;&nbsp; <input type=checkbox name="deselect_ all" value=1
          onClick="select All(false, 'deselect', document.imageF orm['move[]'])">
          -
          De-Select All</td>

          </tr>
          </table>
          </body>
          </html>

          Phil

          "Matt Kruse" <newsgroups@mat tkruse.com> wrote in message news:<c5hid801t mt@news3.newsgu y.com>...[color=blue]
          > "Phil Powell" <soazine@erols. com> wrote:[color=green]
          > > This will work with a checkbox group that has to have this naming
          > > convention for PHP:
          > > I will also have a checkbox that will "select all" and "de-select
          > > all":[/color]
          >
          > Checkboxes don't have onChange() events, they have onclick() events.
          >
          > If you want to ease your pain a bit, you could consider using my "Checkbox
          > Group" library, which would handle all this for you quite easily:
          > http://www.mattkruse.com/javascript/checkboxgroup/[/color]

          Comment

          • Matt Kruse

            #6
            Re: PHP-Javascript method for auto-selecting checkbox groups

            "Phil Powell" <soazine@erols. com> wrote in message
            news:1cdca2a7.0 404140531.46dba 9a7@posting.goo gle.com...[color=blue]
            > Following occurred when my colleague tested on his machine using IE
            > 6.0 SP 4 for Win2000 (I am using Linux/Mozilla):[/color]

            Aren't you missing a <form> tag entirely?

            --
            Matt Kruse
            Javascript Toolbox: http://www.mattkruse.com/javascript/


            Comment

            • The Nordic One

              #7
              Re: PHP-Javascript method for auto-selecting checkbox groups

              No I just forgot to include it within the code I pasted here. Here is
              the corrected code that now works in IE, Opera, Konqueror and Mozilla,
              according to tests I did and a colleague did:

              <form name="imageForm " method=post action=index.ph p
              onSubmit='setHa sSelectedAlbum( ); return willMoveImages( "Any metadata
              associated with the images moved will be altered by moving the images.
              Are you sure?")'>
              <tr>

              <td colspan=6>
              <script type="text/javascript">
              <!--
              function willMoveImages( msg) {
              confirmWindow = window.confirm( msg);
              if (confirmWindow) return true;
              return false;
              }
              //-->
              </script>
              <noscript>
              <!-- Noscript option -->
              </noscript>


              <table border=0 width=100%>
              <tr>
              <td bgcolor=eeeedd colspan=3><a
              href=index.php? section=image&a ction=edit&choo seAlbum=1&album =mu-spin>Go
              back to your previous listing</a></td>

              </tr>
              <tr>
              <td bgcolor=ddddee> Image (click to view)</td> <td
              bgcolor=ddddee> Click to view image metadata if link is found</td> <td
              bgcolor=ddddee> &nbsp;</td>
              </tr>
              <script type="text/javascript">
              <!--

              var hasSelectedAlbu m = false;

              function setHasSelectedA lbum() {
              if (!hasSelectedAl bum) hasSelectedAlbu m = true;
              }

              function selectAll(check State, selectStatus, obj) {
              if (document.image Form.select_all .checked ||
              document.imageF orm.deselect_al l.checked) {
              for (i = 0; i < obj.length; i++) {
              obj[i].checked = checkState;
              }

              if (selectStatus.t oLowerCase() == 'select')
              document.imageF orm.deselect_al l.checked = false;
              if (selectStatus.t oLowerCase() == 'deselect')
              document.imageF orm.select_all. checked = false;
              }
              }
              //-->
              </script>
              <noscript>
              <!-- THERE IS NO NON-JAVASCRIPT EQUIVALENT TO THIS CODE -->

              </noscript>


              <tr>
              <td bgcolor=ffffff> <a
              href="./images/mu-spin/kyrka.jpg">kyrk a.jpg</a></td>
              <td bgcolor=ffffff> Show Metadata</td>
              <td bgcolor=ffffff> <input type=checkbox id="selectable0 " name="move[]"
              value="kyrka.jp g"> - Move?</td>
              </tr>

              <tr>
              <td bgcolor=ffffcc> <a
              href="./images/mu-spin/banner.jpeg">ba nner.jpeg</a></td>
              <td bgcolor=ffffcc> <a
              href=index.php? section=image&a ction=edit&choo seAlbum=1&album =mu-spin&id=
              147>Show Metadata</a></td>
              <td bgcolor=ffffcc> <input type=checkbox id="selectable1 " name="move[]"
              value="banner.j peg"> - Move?</td>
              </tr>
              <tr>
              <td bgcolor=ffffff> <a
              href="./images/mu-spin/kyrka_gray.jpg" >kyrka_gray.jpg </a></td>

              <td bgcolor=ffffff> <a
              href=index.php? section=image&a ction=edit&choo seAlbum=1&album =mu-spin&id=
              145>Show Metadata</a></td>
              <td bgcolor=ffffff> <input type=checkbox id="selectable2 " name="move[]"
              value="kyrka_gr ay.jpg"> - Move?</td>
              </tr>
              <tr>
              <td colspan=3 bgcolor=ddddee>
              <input type=checkbox id="checkall" name="select_al l" value=1
              onClick="select All(true, 'select', document.imageF orm['move[]'])"> -
              Select All&nbsp;&nbsp;

              <input type=checkbox id="uncheckall " name="deselect_ all" value=1
              onClick="select All(false, 'deselect', document.imageF orm['move[]'])"> -
              De-Select All</td>
              </tr>
              This seems to work across the board! Thanx for the tip on "onClick".
              Now to figure out how to determine if the user selected an item from the
              dropdown to change hasSelectedAlbu m. I can't use the "onChange" event
              within the dropdown because the PHP code that generates the dropdown is
              shared across multiple instances.

              Phil


              Phil
              ---------------------------------------
              TCL and PHP: The Superior Web Languages!
              Javascript: Is Cool
              ASP: why?


              *** Sent via Developersdex http://www.developersdex.com ***
              Don't just participate in USENET...get rewarded for it!

              Comment

              • Dr John Stockton

                #8
                Re: PHP-Javascript method for auto-selecting checkbox groups

                JRS: In article <Gg1fc.110637$o R5.109407@pd7tw 3no>, seen in
                news:comp.lang. javascript, Reply Via Newsgroup <reply-to-
                newsgroup@pleas e.com> posted at Wed, 14 Apr 2004 02:23:34 :
                [color=blue]
                > - I'm a newbie (six months)[/color]
                [color=blue]
                >Lastly - I just learned that a mis-use of eval is common - there are
                >other methods that you can use that use less resources and faster (for
                >example, from a numeric point of view you could use something like
                >parseInt() - Do a search on google groups for a recent message I got
                >which explains the reasons - search for "why is eval evil" in
                >comp.lang.java script for a detailed explanation.[/color]

                For that, the OP should be referred to the c.l.j newsgroup FAQ. It's
                generally better to cite a specific reference rather than to call for a
                search.

                --
                © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
                <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                Comment

                Working...