• Resolved Ethan O’Sullivan

    (@ethanosullivan)


    I have WP_DEBUG mode on my local WordPress install for a plugin I am developing.

    In my plugin settings page, I have a list of checkboxes that store the value into an array. While the functions work as expected, whenever I uncheck all of the checkboxes and save those changes I get the Notice: Undefined index: remove_field (line 19) error showing up only when I have debug mode on. Below is the source:

    https://github.com/factmaven/disable-blogging/blob/dev/includes/settings-profile.php

    From doing research online for a similar error, I see that the recommendation is to wrap $_POST['remove_field'] with an isset. However, this does not store the values form the checkboxes in the plugin settings.

    As mentioned above, this error shows up only when all of the checkboxes are unchecked and saved. It looks like there is an issue when saving empty values in the array. Although the plugin settings works as expected, I’d like to fix this.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    You do realize the error is still occurring with debug off, you’re just not notified that it is occurring, right? “Notice” level errors are not fatal, so execution can proceed, but that does not mean your code will work correctly unless the notice error is resolved.

    The way check boxes work when nothing is checked is no value is sent at all, so there is no index to reference. You are correct, isset() or even better, array_key_exists() is the beginning of a solution, but there’s more. When the key is not there or the value is not set, then your script needs to explicitly set all related DB check box values to 0 or false. Something like this:

    if ( array_key_exists('remove_field', $_POST )) {
       // set passed check box values to true, anything else to false
    } else {
       // set all check box values to false
    }

    Thread Starter Ethan O’Sullivan

    (@ethanosullivan)

    Yes I do, I’ve since updated the thread on the WordPress Stack Exchange here. I don’t see where I can reedit my initial post on this thread, even though I was able to make a change a few hours ago.

    Thank you for your input, I was able to fill in the blanks with the logic that you gave me by inserting NULL if all of the checkboxes are unchecked:

    if ( isset( $_POST['dsbl_options'] ) && !empty( $_POST['dsbl_options'] ) ) {
        if ( array_key_exists('remove_field', $_POST )) {
            update_option( 'dsbl_remove', $_POST['remove_field'] );
        }
        else { // When all options are unchecked, set array to null
            update_option( 'dsbl_remove', NULL );
        }
    }

    This got rid of the error for me and everything still works smoothly. Thank you!

    Moderator bcworkz

    (@bcworkz)

    Glad to help 🙂

    I figured the debug thing was just unfortunate wording, but I wanted to be sure you understood just in case.

    These forums have a rather short window where one can edit their post, like 30 minutes or something. Basically, if you don’t proof read and correct right after posting, whatever you said is locked in.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘"Undefined index" error when saving empty array with checkboxes’ is closed to new replies.