Handling Multiple check boxes

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

    Handling Multiple check boxes

    I have a PHP generated page which displays X many records. Each record has
    a checkbox preceding it. The user checks several checkboxes, and hits a
    delete button. All the corresponding records will be deleted.

    But I'm running into difficulty...


    Right now the NAME property of each check box is the primary key of the
    corresponding record. Hence if I know which checkboxes are checked, I
    simply use DELETE using the NAME value.

    Generally speaking, how do I get the server side to see which check boxes
    were checked?

    The check box names may not be sequential, if any records have been deleted
    previously, and the first check box might be a number greater than 0.

    Is there an easy mechanism to do this? Some kind of built in cnotrol array
    allowing me to loop over every check box that was on the form submitted?

    I could store the last and first checkbox number in a hidden input, then
    loop starting/ending at those values, but that may loop over a lot of
    controls that do not exist.


    Thoughts?



    <Ade
    --
    Adrian Parker. Ordained priest. <adrian.parker@ sympatico.ca>

    "A society that views graphic violence as entertainment ...should not be
    surprised when senseless violence shatters the dreams of it's youngest and
    brightest..." - Ensign (March 2004)


  • Martin Andrew Galese

    #2
    Re: Handling Multiple check boxes

    I've solved this problem in the past by giving each of the checkboxes
    the same name, say id, but then setting the value statement to the
    primary key I'm on. When you look in your _POST array, you should find
    that each id that was checked is in a comma separated list. Just process
    this list as you need to loop over the selected primary keys.

    Adrian Parker wrote:[color=blue]
    > I have a PHP generated page which displays X many records. Each record has
    > a checkbox preceding it. The user checks several checkboxes, and hits a
    > delete button. All the corresponding records will be deleted.
    >
    > But I'm running into difficulty...
    >
    >
    > Right now the NAME property of each check box is the primary key of the
    > corresponding record. Hence if I know which checkboxes are checked, I
    > simply use DELETE using the NAME value.
    >
    > Generally speaking, how do I get the server side to see which check boxes
    > were checked?
    >
    > The check box names may not be sequential, if any records have been deleted
    > previously, and the first check box might be a number greater than 0.
    >
    > Is there an easy mechanism to do this? Some kind of built in cnotrol array
    > allowing me to loop over every check box that was on the form submitted?
    >
    > I could store the last and first checkbox number in a hidden input, then
    > loop starting/ending at those values, but that may loop over a lot of
    > controls that do not exist.
    >
    >
    > Thoughts?
    >
    >
    >
    > <Ade[/color]


    --
    Martin
    (Personal email: martin@galese.n et)
    (Work/Academic email: andy@uchicago.e du)

    "As soon as men decide that all means are permitted to fight an evil,
    then their good becomes indistinguishab le from the evil that
    they set out to destroy."
    - Christopher Dawson

    "And this gray spirit yearning in desire
    To follow knowledge like a sinking star,
    Beyond the utmost bound of human thought."
    - Lord Alfred Tennyson

    Comment

    • Adrian Parker

      #3
      Re: Handling Multiple check boxes

      "Martin Andrew Galese" <martin@galese. net> wrote in message
      news:rnVjc.16$4 5.2036@news.uch icago.edu...[color=blue]
      > I've solved this problem in the past by giving each of the checkboxes
      > the same name, say id, but then setting the value statement to the
      > primary key I'm on. When you look in your _POST array, you should find
      > that each id that was checked is in a comma separated list. Just process
      > this list as you need to loop over the selected primary keys.[/color]

      I didn't know the checkbox could have a value. Cool.

      The only way I've used the POST array before was by doing things like:
      if (isset($_POST["controlnam e"]))

      How do you reference it using your method?


      Adrian

      [color=blue]
      >
      > Adrian Parker wrote:[color=green]
      > > I have a PHP generated page which displays X many records. Each record[/color][/color]
      has[color=blue][color=green]
      > > a checkbox preceding it. The user checks several checkboxes, and hits a
      > > delete button. All the corresponding records will be deleted.
      > >
      > > But I'm running into difficulty...
      > >
      > >
      > > Right now the NAME property of each check box is the primary key of the
      > > corresponding record. Hence if I know which checkboxes are checked, I
      > > simply use DELETE using the NAME value.
      > >
      > > Generally speaking, how do I get the server side to see which check[/color][/color]
      boxes[color=blue][color=green]
      > > were checked?
      > >
      > > The check box names may not be sequential, if any records have been[/color][/color]
      deleted[color=blue][color=green]
      > > previously, and the first check box might be a number greater than 0.
      > >
      > > Is there an easy mechanism to do this? Some kind of built in cnotrol[/color][/color]
      array[color=blue][color=green]
      > > allowing me to loop over every check box that was on the form submitted?
      > >
      > > I could store the last and first checkbox number in a hidden input, then
      > > loop starting/ending at those values, but that may loop over a lot of
      > > controls that do not exist.
      > >
      > >
      > > Thoughts?
      > >
      > >
      > >
      > > <Ade[/color]
      >
      >
      > --
      > Martin
      > (Personal email: martin@galese.n et)
      > (Work/Academic email: andy@uchicago.e du)
      >
      > "As soon as men decide that all means are permitted to fight an evil,
      > then their good becomes indistinguishab le from the evil that
      > they set out to destroy."
      > - Christopher Dawson
      >
      > "And this gray spirit yearning in desire
      > To follow knowledge like a sinking star,
      > Beyond the utmost bound of human thought."
      > - Lord Alfred Tennyson[/color]


      Comment

      • Adrian Parker

        #4
        Re: Handling Multiple check boxes


        "Martin Andrew Galese" <martin@galese. net> wrote in message
        news:rnVjc.16$4 5.2036@news.uch icago.edu...[color=blue]
        > I've solved this problem in the past by giving each of the checkboxes
        > the same name, say id, but then setting the value statement to the
        > primary key I'm on. When you look in your _POST array, you should find
        > that each id that was checked is in a comma separated list. Just process
        > this list as you need to loop over the selected primary keys.[/color]

        That doesn't seem to work.

        foreach (array_keys($_P OST) as $key) {
        $$key = $_POST[$key];
        print "$key is ${$key}<br />";
        }


        When I name all the textboxes the same, and make their values the primary
        key of the record to delete, the _POST array just contains the total number
        of checked checkboxes.

        Output:
        "deleteMe is 8
        Delete is Delete selected"

        I check 8 check boxes. Their names are 1, 2, 3, 4, 5, 6, 7, 8

        Either _POST is getting the number of how many are checked, or the value of
        the last one only.



        Adrian


        [color=blue]
        >
        > Adrian Parker wrote:[color=green]
        > > I have a PHP generated page which displays X many records. Each record[/color][/color]
        has[color=blue][color=green]
        > > a checkbox preceding it. The user checks several checkboxes, and hits a
        > > delete button. All the corresponding records will be deleted.
        > >
        > > But I'm running into difficulty...
        > >
        > >
        > > Right now the NAME property of each check box is the primary key of the
        > > corresponding record. Hence if I know which checkboxes are checked, I
        > > simply use DELETE using the NAME value.
        > >
        > > Generally speaking, how do I get the server side to see which check[/color][/color]
        boxes[color=blue][color=green]
        > > were checked?
        > >
        > > The check box names may not be sequential, if any records have been[/color][/color]
        deleted[color=blue][color=green]
        > > previously, and the first check box might be a number greater than 0.
        > >
        > > Is there an easy mechanism to do this? Some kind of built in cnotrol[/color][/color]
        array[color=blue][color=green]
        > > allowing me to loop over every check box that was on the form submitted?
        > >
        > > I could store the last and first checkbox number in a hidden input, then
        > > loop starting/ending at those values, but that may loop over a lot of
        > > controls that do not exist.
        > >
        > >
        > > Thoughts?
        > >
        > >
        > >
        > > <Ade[/color]
        >
        >
        > --
        > Martin
        > (Personal email: martin@galese.n et)
        > (Work/Academic email: andy@uchicago.e du)
        >
        > "As soon as men decide that all means are permitted to fight an evil,
        > then their good becomes indistinguishab le from the evil that
        > they set out to destroy."
        > - Christopher Dawson
        >
        > "And this gray spirit yearning in desire
        > To follow knowledge like a sinking star,
        > Beyond the utmost bound of human thought."
        > - Lord Alfred Tennyson[/color]


        Comment

        • Adrian Parker

          #5
          Re: Handling Multiple check boxes


          "Martin Andrew Galese" <martin@galese. net> wrote in message
          news:rnVjc.16$4 5.2036@news.uch icago.edu...[color=blue]
          > I've solved this problem in the past by giving each of the checkboxes
          > the same name, say id, but then setting the value statement to the
          > primary key I'm on. When you look in your _POST array, you should find
          > that each id that was checked is in a comma separated list. Just process
          > this list as you need to loop over the selected primary keys.[/color]

          I feel supid now.

          This doesn't work. If 10 check boxes all have the same name, the only value
          used is that of that last one. They cascadingly change the value of the one
          checkbox name.

          [color=blue]
          >
          > Adrian Parker wrote:[color=green]
          > > I have a PHP generated page which displays X many records. Each record[/color][/color]
          has[color=blue][color=green]
          > > a checkbox preceding it. The user checks several checkboxes, and hits a
          > > delete button. All the corresponding records will be deleted.
          > >
          > > But I'm running into difficulty...
          > >
          > >
          > > Right now the NAME property of each check box is the primary key of the
          > > corresponding record. Hence if I know which checkboxes are checked, I
          > > simply use DELETE using the NAME value.
          > >
          > > Generally speaking, how do I get the server side to see which check[/color][/color]
          boxes[color=blue][color=green]
          > > were checked?
          > >
          > > The check box names may not be sequential, if any records have been[/color][/color]
          deleted[color=blue][color=green]
          > > previously, and the first check box might be a number greater than 0.
          > >
          > > Is there an easy mechanism to do this? Some kind of built in cnotrol[/color][/color]
          array[color=blue][color=green]
          > > allowing me to loop over every check box that was on the form submitted?
          > >
          > > I could store the last and first checkbox number in a hidden input, then
          > > loop starting/ending at those values, but that may loop over a lot of
          > > controls that do not exist.
          > >
          > >
          > > Thoughts?
          > >
          > >
          > >
          > > <Ade[/color]
          >
          >
          > --
          > Martin
          > (Personal email: martin@galese.n et)
          > (Work/Academic email: andy@uchicago.e du)
          >
          > "As soon as men decide that all means are permitted to fight an evil,
          > then their good becomes indistinguishab le from the evil that
          > they set out to destroy."
          > - Christopher Dawson
          >
          > "And this gray spirit yearning in desire
          > To follow knowledge like a sinking star,
          > Beyond the utmost bound of human thought."
          > - Lord Alfred Tennyson[/color]


          Comment

          • Jan Pieter Kunst

            #6
            Re: Handling Multiple check boxes

            In article <24Vjc.33915$OU .792065@news20. bellglobal.com> ,
            "Adrian Parker" <adrian.parker@ NOSPAMsympatico .ca> wrote:
            [color=blue]
            > Is there an easy mechanism to do this? Some kind of built in cnotrol array
            > allowing me to loop over every check box that was on the form submitted?[/color]

            Make the "name" attribute an array.

            <input type="checkbox" name="checkbox_ id[1]" value="a" />
            <input type="checkbox" name="checkbox_ id[3]" value="b" />
            <input type="checkbox" name="checkbox_ id[56]" value="c" />
            <input type="checkbox" name="checkbox_ id[145]" value="d" />

            If a and c are checked, then after submitting $_REQUEST['checkbox_id']
            is an array containing this: [1] => a [56]=> d.

            JP

            --
            Sorry, <devnull@cauce. org> is een "spam trap".
            E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.

            Comment

            • Adrian Parker

              #7
              Re: Handling Multiple check boxes


              "Jan Pieter Kunst" <devnull@cauce. org> wrote in message
              news:devnull-3C02F7.10204229 042004@news1.ne ws.xs4all.nl...[color=blue]
              > In article <24Vjc.33915$OU .792065@news20. bellglobal.com> ,
              > "Adrian Parker" <adrian.parker@ NOSPAMsympatico .ca> wrote:
              >[color=green]
              > > Is there an easy mechanism to do this? Some kind of built in cnotrol[/color][/color]
              array[color=blue][color=green]
              > > allowing me to loop over every check box that was on the form submitted?[/color]
              >
              > Make the "name" attribute an array.
              >
              > <input type="checkbox" name="checkbox_ id[1]" value="a" />
              > <input type="checkbox" name="checkbox_ id[3]" value="b" />
              > <input type="checkbox" name="checkbox_ id[56]" value="c" />
              > <input type="checkbox" name="checkbox_ id[145]" value="d" />
              >
              > If a and c are checked, then after submitting $_REQUEST['checkbox_id']
              > is an array containing this: [1] => a [56]=> d.[/color]

              How can I loop through it without knowing how many elements it holds?

              Example code?


              Adrian


              Comment

              • Jan Pieter Kunst

                #8
                Re: Handling Multiple check boxes

                In article <Y0dkc.41886$OU .978179@news20. bellglobal.com> ,
                "Adrian Parker" <adrian.parker@ NOSPAMsympatico .ca> wrote:
                [color=blue][color=green]
                > > Make the "name" attribute an array.
                > >
                > > <input type="checkbox" name="checkbox_ id[1]" value="a" />
                > > <input type="checkbox" name="checkbox_ id[3]" value="b" />
                > > <input type="checkbox" name="checkbox_ id[56]" value="c" />
                > > <input type="checkbox" name="checkbox_ id[145]" value="d" />
                > >
                > > If a and c are checked, then after submitting $_REQUEST['checkbox_id']
                > > is an array containing this: [1] => a [56]=> d.[/color][/color]

                (Should be [56] => c, sorry)
                [color=blue]
                > How can I loop through it without knowing how many elements it holds?[/color]

                That's what foreach() is for! See
                <http://nl.php.net/manual/en/control-structures.fore ach.php>.

                Assuming that the form was submitted with method="post":

                foreach($_POST[checkbox_id] as $record_id => $value) {

                do_something_in _database($reco rd_id);
                ...
                }

                JP

                --
                Sorry, <devnull@cauce. org> is een "spam trap".
                E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.

                Comment

                • Adrian Parker

                  #9
                  Re: Handling Multiple check boxes


                  "Jan Pieter Kunst" <devnull@cauce. org> wrote in message
                  news:devnull-3C02F7.10204229 042004@news1.ne ws.xs4all.nl...[color=blue]
                  > In article <24Vjc.33915$OU .792065@news20. bellglobal.com> ,
                  > "Adrian Parker" <adrian.parker@ NOSPAMsympatico .ca> wrote:
                  >[color=green]
                  > > Is there an easy mechanism to do this? Some kind of built in cnotrol[/color][/color]
                  array[color=blue][color=green]
                  > > allowing me to loop over every check box that was on the form submitted?[/color]
                  >
                  > Make the "name" attribute an array.
                  >
                  > <input type="checkbox" name="checkbox_ id[1]" value="a" />
                  > <input type="checkbox" name="checkbox_ id[3]" value="b" />
                  > <input type="checkbox" name="checkbox_ id[56]" value="c" />
                  > <input type="checkbox" name="checkbox_ id[145]" value="d" />
                  >
                  > If a and c are checked, then after submitting $_REQUEST['checkbox_id']
                  > is an array containing this: [1] => a [56]=> d.[/color]

                  This seems to work:

                  foreach(array_k eys($_POST["checkBox_Delet e"]) as $key)
                  {
                  if ($deleteCount == 0)
                  {
                  $deleteThese = "'" . $key . "'";
                  $deleteCount += 1;
                  } else {
                  $deleteThese .= ", '" . $key . "'";
                  }
                  }


                  This look good to you?


                  Adrian


                  Comment

                  • Jan Pieter Kunst

                    #10
                    Re: Handling Multiple check boxes

                    In article <qkdkc.41916$OU .979436@news20. bellglobal.com> ,
                    "Adrian Parker" <adrian.parker@ NOSPAMsympatico .ca> wrote:
                    [color=blue]
                    > This seems to work:
                    >
                    > foreach(array_k eys($_POST["checkBox_Delet e"]) as $key)
                    > {
                    > if ($deleteCount == 0)
                    > {
                    > $deleteThese = "'" . $key . "'";
                    > $deleteCount += 1;
                    > } else {
                    > $deleteThese .= ", '" . $key . "'";
                    > }
                    > }
                    >
                    >
                    > This look good to you?[/color]

                    If you need the number of checked checkboxes, it is faster to get that
                    like this:

                    $deleteCount = count($_POST['checkBox_Delet e']);

                    JP

                    --
                    Sorry, <devnull@cauce. org> is een "spam trap".
                    E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.

                    Comment

                    • Kelly Thompson

                      #11
                      Re: Handling Multiple check boxes

                      On Thu, 29 Apr 2004 22:14:59 +0200
                      Jan Pieter Kunst <devnull@cauce. org> wrote:
                      [color=blue]
                      > In article <Y0dkc.41886$OU .978179@news20. bellglobal.com> ,
                      > "Adrian Parker" <adrian.parker@ NOSPAMsympatico .ca> wrote:
                      >[color=green][color=darkred]
                      > > > Make the "name" attribute an array.
                      > > >
                      > > > <input type="checkbox" name="checkbox_ id[1]" value="a" />
                      > > > <input type="checkbox" name="checkbox_ id[3]" value="b" />
                      > > > <input type="checkbox" name="checkbox_ id[56]" value="c" />
                      > > > <input type="checkbox" name="checkbox_ id[145]" value="d" />
                      > > >
                      > > > If a and c are checked, then after submitting
                      > > > $_REQUEST['checkbox_id'] is an array containing this: [1] => a
                      > > > [56]=> d.[/color][/color]
                      >
                      > (Should be [56] => c, sorry)
                      >[color=green]
                      > > How can I loop through it without knowing how many elements it
                      > > holds?[/color]
                      >
                      > That's what foreach() is for! See
                      > <http://nl.php.net/manual/en/control-structures.fore ach.php>.
                      >
                      > Assuming that the form was submitted with method="post":
                      >
                      > foreach($_POST[checkbox_id] as $record_id => $value) {
                      >
                      > do_something_in _database($reco rd_id);
                      > ...
                      > }[/color]

                      You can also do

                      reset($array);

                      while( list($key,$val) = each( $array ) ) {

                      echo $key . ' => ' . $val;

                      }


                      Comment

                      • Adrian Parker

                        #12
                        Re: Handling Multiple check boxes


                        "Jan Pieter Kunst" <devnull@cauce. org> wrote in message
                        news:devnull-0F9CAE.22343729 042004@news1.ne ws.xs4all.nl...[color=blue]
                        > In article <qkdkc.41916$OU .979436@news20. bellglobal.com> ,
                        > "Adrian Parker" <adrian.parker@ NOSPAMsympatico .ca> wrote:
                        >[color=green]
                        > > This seems to work:
                        > >
                        > > foreach(array_k eys($_POST["checkBox_Delet e"]) as $key)
                        > > {
                        > > if ($deleteCount == 0)
                        > > {
                        > > $deleteThese = "'" . $key . "'";
                        > > $deleteCount += 1;
                        > > } else {
                        > > $deleteThese .= ", '" . $key . "'";
                        > > }
                        > > }
                        > >
                        > >
                        > > This look good to you?[/color]
                        >
                        > If you need the number of checked checkboxes, it is faster to get that
                        > like this:
                        >
                        > $deleteCount = count($_POST['checkBox_Delet e']);[/color]

                        Nah, the deleteCount is just used to see if it's the first element to be
                        added to the string deleteCount.

                        deleteCount is building a single quote and comma delimited string that will
                        be used as argument for a mySQL IN function.



                        Adrian


                        Comment

                        • Jan Pieter Kunst

                          #13
                          Re: Handling Multiple check boxes

                          In article <1Cekc.42137$OU .992778@news20. bellglobal.com> ,
                          "Adrian Parker" <adrian.parker@ NOSPAMsympatico .ca> wrote:
                          [color=blue]
                          > Nah, the deleteCount is just used to see if it's the first element to be
                          > added to the string deleteCount.
                          >
                          > deleteCount is building a single quote and comma delimited string that will
                          > be used as argument for a mySQL IN function.[/color]

                          Ah, I see. In that case this will build the querystring in less code:

                          $querystring = "DELETE FROM ... WHERE ... IN ('" . join("','",
                          array_keys($_PO ST['checkbox_delet e']) . "')";

                          JP

                          --
                          Sorry, <devnull@cauce. org> is een "spam trap".
                          E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.

                          Comment

                          • Adrian Parker

                            #14
                            Re: Handling Multiple check boxes


                            "Jan Pieter Kunst" <devnull@cauce. org> wrote in message
                            news:devnull-D4BAD2.00192430 042004@news1.ne ws.xs4all.nl...[color=blue]
                            > In article <1Cekc.42137$OU .992778@news20. bellglobal.com> ,
                            > "Adrian Parker" <adrian.parker@ NOSPAMsympatico .ca> wrote:
                            >[color=green]
                            > > Nah, the deleteCount is just used to see if it's the first element to be
                            > > added to the string deleteCount.
                            > >
                            > > deleteCount is building a single quote and comma delimited string that[/color][/color]
                            will[color=blue][color=green]
                            > > be used as argument for a mySQL IN function.[/color]
                            >
                            > Ah, I see. In that case this will build the querystring in less code:
                            >
                            > $querystring = "DELETE FROM ... WHERE ... IN ('" . join("','",
                            > array_keys($_PO ST['checkbox_delet e']) . "')";[/color]

                            Cool, thanks.


                            Adrian


                            Comment

                            Working...