11 Replies Latest reply on Dec 13, 2016 9:28 AM by mikebeargie

    PHP – Checkboxes and FileMaker

    FM-K12

      PHP – Checkboxes and FileMaker

       

      I am struggling with how to implement a set of checkboxes on web pages so the full set of selected items get written to the database field on new record creation.

       

      I can get the check box value to successfully display on the new record page, but I can only get one of the check box values 9the last one selected) to write to the database.

       

      Attached are my two pages of code; at this point I am not sure which page needs help.

      When I do the submit record I get this error message:

       

      PHP Warning:  implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in C:\Program Files\FileMaker\FileMaker Server\HTTPServer\conf\vol\add.php on line 32

       

      Any help that you can offer would be most appreciated.

        • 1. Re: PHP – Checkboxes and FileMaker
          mikebeargie

          The issue seems to be that you are not passing a valid array to the implode() function on line 30 of add.php

           

          Looking into addrecord.php, you are not passing the checkboxes to the POST as an array. So update this:

          <input name="SupportTypes" type="checkbox" value="<?php echo $value; ?>">

          to this:

          <input name="SupportTypes[]" type="checkbox" value="<?php echo $value; ?>">

           

          And then in add.php, you will receive an (already formed) array of values with anything that was checked. So change this:

          $SType = $_POST['SupportTypes'];

          $coreCount = 0;

          while ($coreCount <= $SType) {

          $coreCount = $coreCount + 1;

          if(!empty($_POST['SupportTypes'.$coreCount])) {

          $SType [] = $_POST['SupportTypes'.$coreCount];

          }}

          To this (much simpler) line of code:

          $SType = $_POST['SupportTypes'];

           

          Give that a try, I didn't review the rest for errors but that should get you started.

          1 of 1 people found this helpful
          • 2. Re: PHP – Checkboxes and FileMaker
            FM-K12

            Mike –

             

            Thank you so much for your time and expertise.

             

            I just figured this out yesterday and got it 98% working and your post helps confirm for me that I am on the correct road

             

             

            There were two things that help me figure this out, as I am still learning PHP on the fly here.

             

            One was a suggestion by Beverly that I should use GET instead of POST so I could see what was actually passing over to the other page.  This help with the mystery of what was actually being sent or not.

             

             

            The other was a bit of code buried in a PHP page from the old FileMaker Site Assistant.

             

             

            This is the code that helped me begin to get it – inside of the red box.

             

             

             

             

             

             

            The issue that I have discovered with checkboxes is that if the user does not select at least one item then the page crashes.

             

            So some error checking has to be done and to deal with that situation.

             

            Again there were some lines of code in the  that helped to clue me in.

             

             

             

                      //This is to get around the fact that emptied out checkboxes are not passed in to be committed.

                               if(sizeof($fieldEditRecords) != sizeof($recorddata)){

                                                  for( $i=0; $i < sizeof($fieldEditRecords); $i++){

                                                            $isFieldEditable = $fieldEditRecords[$i]->getIsEditable();

                                                            $isCheckBox = $fieldEditRecords[$i]->isCheckBox();

                                                            if(!isset($recorddata[$i]) && $isFieldEditable && $isCheckBox){

                                                                      $recorddata[$i] = "";

                                                            }

                                                  }

                                         }

             

             

             

             

             

             

            So now I have to figure out the best way to do this error checking thing so the entire record still gets committed

             

            Sincerely,

             

            Bill-

             

            Bill Harman

            Technology Consultant

            Montgomery County Public Schools

            850 Hungerford Drive

            Rockville, MD  20850

            (301) 517-5955

            • 3. Re: PHP – Checkboxes and FileMaker
              beverly

              yes! you've discovered one of the problems with the array being passed and the "empty".

               

              I've passed "empty" to be sure there is something. Take Mike's:

              $SType = $_POST['SupportTypes'];

              and change to something like:

              $SType = ($_POST['SupportType'] ? $_POST['SupportType'] : "" );

               

              I cannot test. So if what you have works, go with it.

              beverly

              • 4. Re: PHP – Checkboxes and FileMaker
                FM-K12

                I will test this after lunch; got other fish to fry just now.

                 

                Thanks for the continued support.

                 

                Bill-

                 

                Bill Harman

                Technology Consultant

                Montgomery County Public Schools

                850 Hungerford Drive

                Rockville, MD  20850

                (301) 517-5955

                • 5. Re: PHP – Checkboxes and FileMaker
                  FM-K12

                  Ok – just tested that code and go these results.

                   

                   

                   

                  Error results:

                   

                  PHP Notice:  Undefined index: Checkbox in C:\Program Files\FileMaker\FileMaker Server\HTTPServer\conf\vol\add2.php on line 20

                  PHP Notice:  Undefined index: Checkbox in C:\Program Files\FileMaker\FileMaker Server\HTTPServer\conf\vol\add2.php on line 23

                  PHP Warning:  implode() [function.implode]: Invalid arguments passed in C:\Program Files\FileMaker\FileMaker Server\HTTPServer\conf\vol\add2.php on line 23

                   

                   

                   

                  The offending code:

                   

                  Line 20        $checkbox = ($_POST['Checkbox'] ? $_POST['Checkbox'] : "" );

                   

                  #

                  • 6. Re: PHP – Checkboxes and FileMaker
                    mikebeargie

                    Did you make sure to update Checkbox to Checkbox[] on the form page to indicate an array of checkboxes when posted?

                     

                    Also, PHP is case sensitive, I usually use TitleCase or camelCaseSyntax when referring to fields.

                    • 7. Re: PHP – Checkboxes and FileMaker
                      FM-K12

                      Mike,

                       

                       

                       

                      This is the code on the “addrecord.php” (sending page)page:

                       

                       

                       

                      1. Build the final html

                      2. Format will look something like:

                      3. <input name="Checkbox[]" type="checkbox" value="Academic Support" checked="checked"> Academic Support & Line break character

                       

                      $checkbox .= '

                      ' . $checkBoxValue ."
                      ";

                      }
                      ?>

                      <!

                      • 8. Re: PHP – Checkboxes and FileMaker
                        beverly

                        Then my reply (untested) should not have been made. Apologies!

                        • 9. Re: PHP – Checkboxes and FileMaker
                          FM-K12

                          Since I am on a learning path I am always willing to try something new; sometimes it works and others it does not; but in any case there is usually a lesson there to be learned.

                           

                          I have learned a lot using Mill’s Method - the trial and error path.

                           

                          Thank you for your willingness to support the members of this community.

                           

                          B-

                           

                          Bill Harman

                          Technology Consultant

                          Montgomery County Public Schools

                          850 Hungerford Drive

                          Rockville, MD  20850

                          (301) 517-5955

                          • 10. Re: PHP – Checkboxes and FileMaker
                            FM-K12

                            Thank you Beverly and Mike for your wiling support.

                             

                            It was way too cold and windy here  to work out side this Saturday; so, I stayed inside and solved the last pieces of the puzzle and now I have PHP checkboxes implemented on new record creation that now work even if the user makes no selection in those fields.

                             

                            Bill-

                             

                            Bill Harman

                            Technology Consultant

                            Montgomery County Public Schools

                            850 Hungerford Drive

                            Rockville, MD  20850

                            (301) 517-5955

                            • 11. Re: PHP – Checkboxes and FileMaker
                              mikebeargie

                              Thanks Bill,

                               

                              If you are ever looking for training or assistance, our dev office is down the street in Towson, MD.