Grabbing checkbox values

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

    #1

    Grabbing checkbox values

    I've got a dynamically built form with checkboxes for each element ( a
    list of file names in a directory). I need to grab only those checkboxes
    that are checked, so I can then delete those files.

    Does each checkbox name have to be unique? I was hoping to just group
    them under one name, and select from that array.

    PHP is not my native language - I'm coming at this from Perl, so bear
    with me, things are a little different in that country.

    I've tried:
    foreach ($_REQUEST as $field =$value)
    {
    echo "$field = $value<br>";
    }
    but that just grabs the last entry in the group.

    The line in question is:
    <input type="checkbox" name="DeleteThi s" value="<?php
    echo("$allfiles[$i]")?>">&nbsp;<?p hp echo("$allfiles[$i]")?><br>

    --
    Amer Neely
    w: www.softouch.on.ca/
    b: www.softouch.on.ca/blog/
    Perl | MySQL programming for all data entry forms.
    "We make web sites work!"
  • Amer Neely

    #2
    Re: Grabbing checkbox values

    Amer Neely wrote:
    I've got a dynamically built form with checkboxes for each element ( a
    list of file names in a directory). I need to grab only those checkboxes
    that are checked, so I can then delete those files.
    >
    Does each checkbox name have to be unique? I was hoping to just group
    them under one name, and select from that array.
    >
    PHP is not my native language - I'm coming at this from Perl, so bear
    with me, things are a little different in that country.
    >
    I've tried:
    foreach ($_REQUEST as $field =$value)
    {
    echo "$field = $value<br>";
    }
    but that just grabs the last entry in the group.
    >
    The line in question is:
    <input type="checkbox" name="DeleteThi s" value="<?php
    echo("$allfiles[$i]")?>">&nbsp;<?p hp echo("$allfiles[$i]")?><br>
    I've changed the code for the above line so the name now is unique for
    each element. That now lets me grab whichever one/s are checked. But,
    another problem has arisen.

    I also have a JavaScript toggle to 'Select/Unselect All' checkboxes,
    which works fine. But the 'checked' status doesn't seem to be getting
    passed to the PHP code. So, when I display the items checked, it is
    incorrect. Refreshing the page also doesn't seem to correct it. Only
    physically clicking the checkboxes seems to work.

    --
    Amer Neely
    w: www.softouch.on.ca/
    b: www.softouch.on.ca/blog/
    Perl | MySQL programming for all data entry forms.
    "We make web sites work!"

    Comment

    • flamer die.spam@hotmail.com

      #3
      Re: Grabbing checkbox values


      Amer Neely wrote:
      I've got a dynamically built form with checkboxes for each element ( a
      list of file names in a directory). I need to grab only those checkboxes
      that are checked, so I can then delete those files.
      >
      Does each checkbox name have to be unique? I was hoping to just group
      them under one name, and select from that array.
      >
      PHP is not my native language - I'm coming at this from Perl, so bear
      with me, things are a little different in that country.
      >
      I've tried:
      foreach ($_REQUEST as $field =$value)
      {
      echo "$field = $value<br>";
      }
      but that just grabs the last entry in the group.
      >
      The line in question is:
      <input type="checkbox" name="DeleteThi s" value="<?php
      echo("$allfiles[$i]")?>">&nbsp;<?p hp echo("$allfiles[$i]")?><br>
      >
      --
      Amer Neely
      w: www.softouch.on.ca/
      b: www.softouch.on.ca/blog/
      Perl | MySQL programming for all data entry forms.
      "We make web sites work!"
      in html, if your checkbox is called "blah123" then when its checked it
      will have the variable name of blah123 with a value of blah123, if it
      is unchecked then blah123==null. the easy way is:

      if (!empty($_POST['blah123'])) { do stuff }

      Flamer.

      Comment

      • Rogats

        #4
        Re: Grabbing checkbox values

        php is not to "grab" the checkboxes, it comes and goes as lessons for
        the yum mathematical calus on webpage stuff!
        Use the html to expose your own web pages!

        Amer Neely wrote:
        I've got a dynamically built form with checkboxes for each element ( a
        list of file names in a directory). I need to grab only those checkboxes
        that are checked, so I can then delete those files.
        >
        Does each checkbox name have to be unique? I was hoping to just group
        them under one name, and select from that array.
        >
        PHP is not my native language - I'm coming at this from Perl, so bear
        with me, things are a little different in that country.
        >
        I've tried:
        foreach ($_REQUEST as $field =$value)
        {
        echo "$field = $value<br>";
        }
        but that just grabs the last entry in the group.
        >
        The line in question is:
        <input type="checkbox" name="DeleteThi s" value="<?php
        echo("$allfiles[$i]")?>">&nbsp;<?p hp echo("$allfiles[$i]")?><br>
        >
        --
        Amer Neely
        w: www.softouch.on.ca/
        b: www.softouch.on.ca/blog/
        Perl | MySQL programming for all data entry forms.
        "We make web sites work!"

        Comment

        • Amer Neely

          #5
          Re: Grabbing checkbox values

          flamer die.spam@hotmai l.com wrote:
          Amer Neely wrote:
          >I've got a dynamically built form with checkboxes for each element ( a
          >list of file names in a directory). I need to grab only those checkboxes
          >that are checked, so I can then delete those files.
          >>
          >Does each checkbox name have to be unique? I was hoping to just group
          >them under one name, and select from that array.
          >>
          >PHP is not my native language - I'm coming at this from Perl, so bear
          >with me, things are a little different in that country.
          >>
          >I've tried:
          > foreach ($_REQUEST as $field =$value)
          > {
          > echo "$field = $value<br>";
          > }
          >but that just grabs the last entry in the group.
          >>
          >The line in question is:
          ><input type="checkbox" name="DeleteThi s" value="<?php
          >echo("$allfile s[$i]")?>">&nbsp;<?p hp echo("$allfiles[$i]")?><br>
          >
          in html, if your checkbox is called "blah123" then when its checked it
          will have the variable name of blah123 with a value of blah123, if it
          is unchecked then blah123==null. the easy way is:
          >
          if (!empty($_POST['blah123'])) { do stuff }
          >
          Flamer.
          >
          Hmmm. OK, so I will need to also walk through each of the new input
          names, and check them with the 'empty' function. I'll work on that. Thanks.


          --
          Amer Neely
          w: www.softouch.on.ca/
          b: www.softouch.on.ca/blog/
          Perl | MySQL programming for all data entry forms.
          "We make web sites work!"

          Comment

          • Amer Neely

            #6
            Re: Grabbing checkbox values

            flamer die.spam@hotmai l.com wrote:
            Amer Neely wrote:
            >I've got a dynamically built form with checkboxes for each element ( a
            >list of file names in a directory). I need to grab only those checkboxes
            >that are checked, so I can then delete those files.
            >>
            >Does each checkbox name have to be unique? I was hoping to just group
            >them under one name, and select from that array.
            >>
            >PHP is not my native language - I'm coming at this from Perl, so bear
            >with me, things are a little different in that country.
            >>
            >I've tried:
            > foreach ($_REQUEST as $field =$value)
            > {
            > echo "$field = $value<br>";
            > }
            >but that just grabs the last entry in the group.
            >>
            >The line in question is:
            ><input type="checkbox" name="DeleteThi s" value="<?php
            >echo("$allfile s[$i]")?>">&nbsp;<?p hp echo("$allfiles[$i]")?><br>
            >>
            >--
            >Amer Neely
            >w: www.softouch.on.ca/
            >b: www.softouch.on.ca/blog/
            >Perl | MySQL programming for all data entry forms.
            >"We make web sites work!"
            >
            in html, if your checkbox is called "blah123" then when its checked it
            will have the variable name of blah123 with a value of blah123, if it
            is unchecked then blah123==null. the easy way is:
            >
            if (!empty($_POST['blah123'])) { do stuff }
            >
            Flamer.
            >
            I can now grab the selected elements, but only if they are selected by
            mouse clicks. If I use the JavaScript toggle to select/unselect all,
            nothing gets passed to the PHP. This works in Perl, so it has to be
            doable in PHP I would think.

            --
            Amer Neely
            w: www.softouch.on.ca/
            b: www.softouch.on.ca/blog/
            Perl | MySQL programming for all data entry forms.
            "We make web sites work!"

            Comment

            • Michael Fesser

              #7
              Re: Grabbing checkbox values

              ..oO(flamer die.spam@hotmai l.com)
              >in html, if your checkbox is called "blah123" then when its checked it
              >will have the variable name of blah123 with a value of blah123
              It will have the value of the 'value' attribute.
              >if it
              >is unchecked then blah123==null.
              If unchecked then the checkbox is not submitted at all.

              Micha

              Comment

              • Michael Fesser

                #8
                Re: Grabbing checkbox values

                ..oO(Amer Neely)
                >I've got a dynamically built form with checkboxes for each element ( a
                >list of file names in a directory). I need to grab only those checkboxes
                >that are checked, so I can then delete those files.
                Only checked checkboxes will be submitted by the browser.
                >Does each checkbox name have to be unique?
                No, you can use the same name, but different values. To make this work
                in PHP you have to add brackets to the name, see below.
                >I've tried:
                foreach ($_REQUEST as $field =$value)
                {
                echo "$field = $value<br>";
                }
                >but that just grabs the last entry in the group.
                >
                >The line in question is:
                ><input type="checkbox" name="DeleteThi s" value="<?php
                >echo("$allfile s[$i]")?>">&nbsp;<?p hp echo("$allfiles[$i]")?><br>
                Some things:

                1) You should not use $_REQUEST. This array contains data from different
                sources, so you don't know where all the values actually came from. Use
                the array that corresponds with the used method in your form, which
                should be $_POST in this case.

                2) Instead of $allfiles[$i] you can just use the variable $i as the
                value of your checkboxes, that's enough. When processing the form use
                that value to lookup the filenames in your $allfiles array.

                3) To process multiple form controls that share the same name you have
                to add square brackets to their name, e.g. "DeleteThis[]". This causes
                PHP to store all the values in an array instead of a scalar.

                So the code could look something like this:

                if (isset($_POST['DeleteThis')) {
                foreach ($_POST['DeleteThis'] as $index) {
                if (isset($allfile s[$index])) {
                // perform action to delete the file
                }
                }
                }

                The code for the checkboxes like I would probably write it:

                printf("<label> <index type='checkbox' name='DeleteThi s[]'
                value='%u'>&nbs p;%s</label><br>\n", $i, $allfiles[$i]);

                HTH
                Micha

                Comment

                • dudordoo123456@yahoo.dk

                  #9
                  Re: Grabbing checkbox values

                  script, not html, this way post becomes fully clear.

                  Rogats wrote:
                  php is not to "grab" the checkboxes, it comes and goes as lessons for
                  the yum mathematical calus on webpage stuff!
                  Use the html to expose your own web pages!
                  >
                  Amer Neely wrote:
                  I've got a dynamically built form with checkboxes for each element ( a
                  list of file names in a directory). I need to grab only those checkboxes
                  that are checked, so I can then delete those files.

                  Does each checkbox name have to be unique? I was hoping to just group
                  them under one name, and select from that array.

                  PHP is not my native language - I'm coming at this from Perl, so bear
                  with me, things are a little different in that country.

                  I've tried:
                  foreach ($_REQUEST as $field =$value)
                  {
                  echo "$field = $value<br>";
                  }
                  but that just grabs the last entry in the group.

                  The line in question is:
                  <input type="checkbox" name="DeleteThi s" value="<?php
                  echo("$allfiles[$i]")?>">&nbsp;<?p hp echo("$allfiles[$i]")?><br>

                  --
                  Amer Neely
                  w: www.softouch.on.ca/
                  b: www.softouch.on.ca/blog/
                  Perl | MySQL programming for all data entry forms.
                  "We make web sites work!"

                  Comment

                  • Deniel Keplar

                    #10
                    Re: Grabbing checkbox values

                    after you succeed, post your reply, I want to know how also.

                    Amer Neely wrote:
                    >
                    Hmmm. OK, so I will need to also walk through each of the new input
                    names, and check them with the 'empty' function. I'll work on that. Thanks.
                    >

                    Comment

                    • Amer Neely

                      #11
                      Re: Grabbing checkbox values

                      Michael Fesser wrote:
                      .oO(Amer Neely)
                      >
                      >I've got a dynamically built form with checkboxes for each element ( a
                      >list of file names in a directory). I need to grab only those checkboxes
                      >that are checked, so I can then delete those files.
                      >
                      Only checked checkboxes will be submitted by the browser.
                      >
                      >Does each checkbox name have to be unique?
                      >
                      No, you can use the same name, but different values. To make this work
                      in PHP you have to add brackets to the name, see below.
                      >
                      >I've tried:
                      > foreach ($_REQUEST as $field =$value)
                      > {
                      > echo "$field = $value<br>";
                      > }
                      >but that just grabs the last entry in the group.
                      >>
                      >The line in question is:
                      ><input type="checkbox" name="DeleteThi s" value="<?php
                      >echo("$allfile s[$i]")?>">&nbsp;<?p hp echo("$allfiles[$i]")?><br>
                      >
                      Some things:
                      >
                      1) You should not use $_REQUEST. This array contains data from different
                      sources, so you don't know where all the values actually came from. Use
                      the array that corresponds with the used method in your form, which
                      should be $_POST in this case.
                      >
                      2) Instead of $allfiles[$i] you can just use the variable $i as the
                      value of your checkboxes, that's enough. When processing the form use
                      that value to lookup the filenames in your $allfiles array.
                      >
                      3) To process multiple form controls that share the same name you have
                      to add square brackets to their name, e.g. "DeleteThis[]". This causes
                      PHP to store all the values in an array instead of a scalar.
                      >
                      So the code could look something like this:
                      >
                      if (isset($_POST['DeleteThis')) {
                      foreach ($_POST['DeleteThis'] as $index) {
                      if (isset($allfile s[$index])) {
                      // perform action to delete the file
                      }
                      }
                      }
                      >
                      The code for the checkboxes like I would probably write it:
                      >
                      printf("<label> <index type='checkbox' name='DeleteThi s[]'
                      value='%u'>&nbs p;%s</label><br>\n", $i, $allfiles[$i]);
                      >
                      HTH
                      Micha
                      I can get the checked boxes only if the user clicks them with the mouse.
                      Which is fine, but I also have some JavaScript that lets me toggle the
                      boxes 'select / unselect all'. This is the part that is now giving me
                      grief. In Perl I can do:

                      my @infiles=param( 'DeleteThis');

                      which gets an array of all checked entries in the 'DeleteThis' group.
                      And this works with my JavaScript code. The 'param' function is part of
                      a perl module (CGI.pm) that deals with form parsing. Is there anything
                      like that for PHP? It would make life so much easier.

                      --
                      Amer Neely
                      w: www.softouch.on.ca/
                      b: www.softouch.on.ca/blog/
                      Perl | MySQL programming for all data entry forms.
                      "We make web sites work!"

                      Comment

                      • Jerry Stuckle

                        #12
                        Re: Grabbing checkbox values

                        Amer Neely wrote:
                        flamer die.spam@hotmai l.com wrote:
                        >
                        >Amer Neely wrote:
                        >>
                        >>I've got a dynamically built form with checkboxes for each element ( a
                        >>list of file names in a directory). I need to grab only those checkboxes
                        >>that are checked, so I can then delete those files.
                        >>>
                        >>Does each checkbox name have to be unique? I was hoping to just group
                        >>them under one name, and select from that array.
                        >>>
                        >>PHP is not my native language - I'm coming at this from Perl, so bear
                        >>with me, things are a little different in that country.
                        >>>
                        >>I've tried:
                        >> foreach ($_REQUEST as $field =$value)
                        >> {
                        >> echo "$field = $value<br>";
                        >> }
                        >>but that just grabs the last entry in the group.
                        >>>
                        >>The line in question is:
                        >><input type="checkbox" name="DeleteThi s" value="<?php
                        >>echo("$allfil es[$i]")?>">&nbsp;<?p hp echo("$allfiles[$i]")?><br>
                        >>
                        >>
                        >in html, if your checkbox is called "blah123" then when its checked it
                        >will have the variable name of blah123 with a value of blah123, if it
                        >is unchecked then blah123==null. the easy way is:
                        >>
                        >if (!empty($_POST['blah123'])) { do stuff }
                        >>
                        >Flamer.
                        >>
                        >
                        Hmmm. OK, so I will need to also walk through each of the new input
                        names, and check them with the 'empty' function. I'll work on that. Thanks.
                        >
                        >
                        Amer,

                        No. It's much easier than that. Have all the checkboxes names the
                        same, with square brackets ( "[]" ) following. The you will be able to
                        use an array on the PHP side, i.e.


                        <input type="checkbox" name="DeleteThi s[]" value="<?php
                        echo("$allfiles[$i]")?>">&nbsp;<?p hp echo("$allfiles[$i]")?><br>>


                        To process the input, all you need is:

                        foreach ($_POST['DeleteThis'] as $delfile) {
                        // Validate the filename is OK here
                        unlink $delfile;
                        }

                        This will work whether the checkbox is set via a mouse click or javascript.

                        Of course, you will want to validate your filenames. That part I left
                        out because I have no idea what the criteria is.

                        --
                        =============== ===
                        Remove the "x" from my email address
                        Jerry Stuckle
                        JDS Computer Training Corp.
                        jstucklex@attgl obal.net
                        =============== ===

                        Comment

                        • Amer Neely

                          #13
                          Re: Grabbing checkbox values

                          Deniel Keplar wrote:
                          after you succeed, post your reply, I want to know how also.
                          >
                          Amer Neely wrote:
                          >Hmmm. OK, so I will need to also walk through each of the new input
                          >names, and check them with the 'empty' function. I'll work on that. Thanks.
                          >>
                          >
                          Will do.

                          In the mean time, here is the JS code I use to toggle checkboxes on or
                          off. You will have to edit to suit your situation. Specifically, this is
                          hard-coded to work on the first form on a page (forms[0]) so you would
                          change that to either a form name or specify which form you want to work
                          with.

                          function CheckAll(value) // toggles checkboxes on/off
                          {
                          for (i=0; i < document.forms[0].length; i++)
                          {
                          document.forms[0].elements[i].value);
                          if ( document.forms[0].elements[i].name =~ 'DeleteThis')
                          {
                          document.forms[0].elements[i].checked=value;
                          }
                          }
                          }

                          The function is called like this:
                          <a href="#" style="color:re d; text-decoration:none "
                          onClick="CheckA ll(1);return false;">Select All</a>&nbsp;•&nbsp; <a
                          style="color:re d; text-decoration:none " href="#" onClick="CheckA ll(0);
                          return false;">Unselec t All<br /></a>

                          --
                          Amer Neely
                          w: www.softouch.on.ca/
                          b: www.softouch.on.ca/blog/
                          Perl | MySQL programming for all data entry forms.
                          "We make web sites work!"

                          Comment

                          • Amer Neely

                            #14
                            Re: Grabbing checkbox values

                            Jerry Stuckle wrote:
                            Amer Neely wrote:
                            >flamer die.spam@hotmai l.com wrote:
                            >>
                            >>Amer Neely wrote:
                            >>>
                            >>>I've got a dynamically built form with checkboxes for each element ( a
                            >>>list of file names in a directory). I need to grab only those
                            >>>checkboxes
                            >>>that are checked, so I can then delete those files.
                            >>>>
                            >>>Does each checkbox name have to be unique? I was hoping to just group
                            >>>them under one name, and select from that array.
                            >>>>
                            >>>PHP is not my native language - I'm coming at this from Perl, so bear
                            >>>with me, things are a little different in that country.
                            >>>>
                            >>>I've tried:
                            >>> foreach ($_REQUEST as $field =$value)
                            >>> {
                            >>> echo "$field = $value<br>";
                            >>> }
                            >>>but that just grabs the last entry in the group.
                            >>>>
                            >>>The line in question is:
                            >>><input type="checkbox" name="DeleteThi s" value="<?php
                            >>>echo("$allfi les[$i]")?>">&nbsp;<?p hp echo("$allfiles[$i]")?><br>
                            >>>
                            >>>
                            >>in html, if your checkbox is called "blah123" then when its checked it
                            >>will have the variable name of blah123 with a value of blah123, if it
                            >>is unchecked then blah123==null. the easy way is:
                            >>>
                            >>if (!empty($_POST['blah123'])) { do stuff }
                            >>>
                            >>Flamer.
                            >>>
                            >>
                            >Hmmm. OK, so I will need to also walk through each of the new input
                            >names, and check them with the 'empty' function. I'll work on that.
                            >Thanks.
                            >>
                            >>
                            >
                            Amer,
                            >
                            No. It's much easier than that. Have all the checkboxes names the
                            same, with square brackets ( "[]" ) following. The you will be able to
                            use an array on the PHP side, i.e.
                            >
                            >
                            <input type="checkbox" name="DeleteThi s[]" value="<?php
                            echo("$allfiles[$i]")?>">&nbsp;<?p hp echo("$allfiles[$i]")?><br>>
                            >
                            >
                            To process the input, all you need is:
                            >
                            foreach ($_POST['DeleteThis'] as $delfile) {
                            // Validate the filename is OK here
                            unlink $delfile;
                            }
                            >
                            This will work whether the checkbox is set via a mouse click or javascript.
                            >
                            Of course, you will want to validate your filenames. That part I left
                            out because I have no idea what the criteria is.
                            >
                            OK, I have your code inserted, but it gives me an error when I use my
                            'Select All' JS:

                            Warning: Invalid argument supplied for foreach() in show_spam4.php on
                            line 116

                            If I click any box with the mouse it returns 'on' for the output.

                            [116] foreach ($_POST['DeleteThis'] as $value)
                            {
                            echo "$value<br> ";
                            }

                            --
                            Amer Neely
                            w: www.softouch.on.ca/
                            b: www.softouch.on.ca/blog/
                            Perl | MySQL programming for all data entry forms.
                            "We make web sites work!"

                            Comment

                            • Amer Neely

                              #15
                              Re: Grabbing checkbox values

                              Deniel Keplar wrote:
                              after you succeed, post your reply, I want to know how also.
                              >
                              Amer Neely wrote:
                              >Hmmm. OK, so I will need to also walk through each of the new input
                              >names, and check them with the 'empty' function. I'll work on that. Thanks.
                              >>
                              >
                              Found a couple of good articles here:
                              http://www.webreference.com/programming/php/

                              Follow Part 1 and Part 2.

                              --
                              Amer Neely
                              w: www.softouch.on.ca/
                              b: www.softouch.on.ca/blog/
                              Perl | MySQL programming for all data entry forms.
                              "We make web sites work!"

                              Comment

                              Working...