1

I'm writing a plugin to edit my profile page.

How can I save data of an input field to the database and then display it?

This is my code. The first part is the display in HTML:

<form method ="POST">
    <div id= 'dynamic_input_groups'>
        <input type='text' name='group[]' id='group[]' class="regular-text"
            value="<?php echo esc_attr( get_the_author_meta( 'group', $user->ID ) ); ?>" />

        <input type="button" class="button-primary" value="Add"
            onClick="addInput('dynamic_input_groups');">
    </div>
</form>

And the second part is the script for the button click and the input field:

<script>
var counter = 1;
var limit = 15;
function addInput(divName) {
    if (counter == limit) {
        alert("You have reached the limit of adding " + counter + " inputs");
    } else {
        var newdiv = document.createElement('div');
        newdiv.innerHTML = " <br><input type='text' name='group[]'' class='regular-text'>";
        document.getElementById(divName).appendChild(newdiv);
        counter++;
    }
}
</script>

And the final part saves the data to the database:

update_usermeta( $user_id, 'group', $_POST['group'] );

1 Answer 1

1

Don't use update_usermeta, it is deprecated, update_user_meta is the one to use.

You get the previously saved value out with get_user_meta.

<input type='text' name='group[]' id='group[]' class="regular-text"
    value="<?php echo esc_attr( get_user_meta( $user->ID, 'group', true ) ); ?>" />
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.