0

There's something wrong with how I'm managing my checkbox with in my HTML input element - as in the database, the value stays at 1, and the form always shows 1, no matter if it's un-ticked.

Any idea please?

I've googled a lot but I'm also struggling to understand the concept of how the checkbox works with the setting of (in HTML) and storing of (in PHP), as most of the results are for post_meta, but this is for user_meta key / value.

<?php

// set the HTML for the inputs
function show_extra_profile_fields( $user ) {
    $is_user_archived = get_user_meta( $user->ID, 'archived_user', true );
    ?>
    <table class="form-table">
        <tr>
            <th><label for="archived_user"><?php esc_html_e( 'Archive user', 'xlearn' ); ?></label></th>
            <td>
                <input type="checkbox"
                       id="archived_user"
                    name="archived_user"
                    class="checkbox"
                    value="yes"
                    <?php if (get_user_meta( $user->ID, 'archived_user', true ) == "1") echo "checked" ;?>
                />
            </td>
        </tr>
    </table>
    <?php
}
add_action( 'edit_user_profile', 'show_extra_profile_fields' );

// update the user meta on save
function update_profile_fields( $user_id ) {
    if ( !current_user_can( 'edit_user', $user_id ) ) {
        return false;
    }
    if ( isset( $_POST['archived_user'] ) ) {
         update_user_meta( $user_id, 'archived_user', $_POST['archived_user'] );
    }    
}
add_action( 'edit_user_profile_update', 'update_profile_fields' ); 
mx0
  • 6,445
  • 12
  • 49
  • 54
Brett
  • 359
  • 3
  • 16
  • 1
    Check against the value of `$_POST['archived_user']`. I've had checkboxes come in with the value "on" or "off" and encountered the same problem. – mcon Oct 26 '18 at 17:04
  • 1
    See [this SO post](https://stackoverflow.com/questions/11424037/does-input-type-checkbox-only-post-data-if-its-checked). Checkbox values are only submitted when they're checked (meaning they won't even be set in `$_POST` at all). Their default value is `"on"` but since you've set that to `"yes"` you should be checking for that value instead. You are correctly handling the `checked` HTML bit but you may have to adjust your equality statement. – sheng Oct 26 '18 at 17:07

1 Answers1

0

Ok so I eventually figured this out - I changed some of the form inputs & also some of the PHP validation:

I was also missing: form method="post" in the FORM tag

    // set the HTML for the inputs
    function show_extra_profile_fields( $user ) {
        $is_user_archived = get_user_meta( $user->ID, 'archived_user', true );

        /* if you want a heading
        // insert below into HTML area after PHP end tag
        <h3><?php esc_html_e( 'Personal Information', 'xlearn' ); ?></h3> */

        ?>
        <table class="form-table">
            <tr>
                <th><label for="archived_user"><?php esc_html_e( 'Archive user', 'xlearn' ); ?></label></th>
                <td>
                    <form method="post">
                        <input type="checkbox"
                               id="archived_user"
                            name="archived_user"
                            class="checkbox"
                            value="checked"

                            <?php
                                // GET VALUES FROM THE DATABSE:

                                // if meta key is set
                                if ($is_user_archived == "1") {
                                    echo 'value="1"';
                                    echo 'checked="1"';
                                }
                                // if meta key is not present or = 0
                                else if (($is_user_archived == "") | ($is_user_archived == "0")) {
                                    echo 'value="0"';
                                }
                            ?>

                            />
                    </form>
                    <label for="archived_user">This will disable the user from logging in, but will not delete the user</label>
                </td>
            </tr>
        </table>
        <?php
    }
    add_action( 'edit_user_profile', 'show_extra_profile_fields' );

Take note of the 2 x $_POST['archived_user'] areas below:

    // update the user meta on save
    function update_profile_fields( $user_id ) {
        // check for capabilities
        if ( !current_user_can( 'edit_user', $user_id ) ) {
            return false;
        }
        // ticked
        if ( ( isset( $_POST['archived_user'] ))) {
             update_user_meta( $user_id, 'archived_user', "1" );
        }
        // not ticked
        if ( ( !isset( $_POST['archived_user'] ))) {
             update_user_meta( $user_id, 'archived_user', "0" );
        }
    }
    add_action( 'edit_user_profile_update', 'update_profile_fields' ); 
Brett
  • 359
  • 3
  • 16