I posted earlier in regards to trying to work with checkboxes in PHP and MySQL, and ... well, I'm not getting anywhere.

Here's the scenario:

Fields in a MySQL database called rst_HP, rst_PUB etc. Field type set to ENUM 'Y','N'.

Checkboxes are in my html document as such:

<input type="checkbox" name="HP" value=N> HP
<input type="checkbox" name="HP" value=N> PUB

Now, when a user is adding a new "account", they can select these checkboxes for various purposes. Then, if/when they return to edit the account, the checkboxes are repopulated with the value they chose.

My biggest problem is with the Yes/No values. How do I work with the ENUM values so that they correspond to the checkbox being checked or not?

Thanks in advance for any help provided.
B

    Hi,

    As far as I know are present in the $_POST array when they have been checked. Otherwise they are not.

    So you can check wether a certain variable in the post array has value "" -> Then it was not checked. Else it was checked.

    With that info you can insert it in the database.

    J.

      True, they show up when I use
      print_r($_POST);

      but, they naturally show up as "N" (since I have the default set to "N") even when they are "Y" in the database.

      Do I have to set the default value?

      Argh, I is confusled...

      B

        Yes checkboxes, radio buttons and drop down menus are very, very annoying to populate, since their value and 'selected' attribute are not the same thing (as with text boxes). You need to use regexp to check the value, and then add the correct attribute in (checked, selected or whatever). I use an id attribute to find the correct element.

        e.g.

        <select id=\"someid\" ... rest of element><options .... ></select>

        Then I can use something like

        $string = preg_replace("/(<select id=\"someid\".*?>.*?)(<option value=\"$valueiwanttofind\")>/si", "$1$2 selected>", $string);
        

        This first matches the element type plus the id, then matches anything at all until it comes across the <option> element which has the correct value. The replace part can then add in the 'selected' attribute. You need to ensure that there are no existing 'selected' attributes or you'll end up with more than one. Another regexp can take care of that.

        Quick note, I work using HTML and PHP in separate files. If you use combined HTML and PHP then you don't really have a problem. Just add in the selected/checked attribute when creating the HTML.

        hth

          if (isset($_POST['HP'])) $hp = 'Y';
          else $hp = 'N';

            I may be wrong but it seems that the poster wants to prefill an HTML form, not get the actual values in PHP.

              That's what I got from it, Shrike.

              To accomplish this, have an enum field-type in the database with the possible values 'on','null'. Have the default value as null also.

              When posting the initial data to the DB, if the checkbox is checked, the value will be 'on'. If not checked, it will be null.

              When pulling the data from the db, run a conditional statement to check if the checkbox value is on. i.e.

              if($dbCheckVal=='on') {
                $strIsChecked="checked=\"checked\"";
              }
              

              Inside the checkbox html. i.e.

              <input type="checkbox" ".$strIsChecked." name="foo" />

                Thanks for the suggestions folks, but unfortunately I'm still having issues with this...

                I have my checkboxes repopulating via the following:

                if (isset($row['rst_Federal'])){
                $strIsChecked = 'checked';
                }

                <input type="checkbox" <? echo $strIsChecked ?> name="Federal">

                But, I'm not sure it's the best method. As it stands, I'll have 14 checkboxes on this page, all pulled from seperate fields in a MySQL table. So, rst_HP can be "on" or "null", rst_PUB can be "on" or "null" etc.

                That being said, would I have to write a if(isset function for each one?

                Also, when inserting this information into the DB, the checkbox isn't posting a value (ie. "on"). It's coming up blank.

                pulls hair

                There has to be an easier way, no?

                Sincerely,
                B

                  I believe I've found a solution using information from this post as well as some code I used to work with radio buttons.

                  Here's what I've done:

                  First, I changed my MySQL enum table as suggest (to 'on','null') with the default value set to null.

                  Then, for the checkboxes I did the following:

                  <input type="checkbox" name="Federal" <?php echo ($row[rst_Federal] == 'on' ? " CHECKED" : ""); ?>> Federal
                  

                  These will now re-populate the checkbox if set to 'on' in the MySQL table rst_Federal.

                  When POSTing this information, checkboxes that are checked will post the value 'on' while unchecked boxes will not post anything at all.

                  Thanks everyone for your help. I struggled with these damn boxes for far too long, and once again the pHpBuilder folks saved my arse. 😃

                  Cheers
                  B

                    Write a Reply...