• Hi,

    In my registration page I put a radio button to check the user_role and I want to know how to save the value of the ‘checked’ radio button in my database.

    I already added a part of code to save this value in usermeta table but nothing appears!

    this is a part of my php code:

    <?php
    $role_info = $_POST['role_info'];
    ?>
    <label for="user-role-type"><?php _e('What are you?',$current_theme_locale_name) ?></label>
    <input type="radio" class="do_input" name="role_info" id="role_info" value="User" checked="checked"><?php echo __( 'User', $current_theme_locale_name ); ?>
    <br />
    <input type="radio" class="do_input" name="role_info" id="role_info" value="Seller"><?php echo __( 'Seller', $current_theme_locale_name ); ?>
    <?php
    update_user_meta($user_id, 'role_info', $role_info);
    ?>

    Any help will be appreciate, Thanks in advance

Viewing 1 replies (of 1 total)
  • Moderator Marius L. J.

    (@clorith)

    Hi,

    It seems you are running all the logic in one page, which won’t end well ??

    What I do when I have a form is I add an action to the init event where I look for my form, and do the logic of updating database entries and such.

    So your code above would be split over two files, depending on if this is a theme or plugin you’d have to determine the file that best suits it based on that.

    The logic behind the scenes

    function wp367128_init_action() {
    	if ( isset( $_POST['wp367128_form'] ) ) {
    		update_user_meta( $user_id, 'role_info', isset( $_POST['wp367128_checkbox'] ) );
    	}
    }
    add_action( 'init', 'wp_367128_init_action' );

    The front facing form

    <?php
    	$role_info = get_user_meta( $user_id, 'role_info', true );
    ?>
    <form action="" method="post">
    	<input type="hidden" name="wp367125_form" value="hidden value to check if our form is submitted">
    
    	<label>
    		<input type="checkbox" name="wp_367128_checkbox" <?php checked( true, $role_info ); ?> >
    		<?php _e( 'User', 'textdomain' ); ?>
    	</label>
    </form>

    This is obviously not code meant to be used directly as is ??

    Also for your text domain (awesome, you’re making this translatable!) please use the actual string, and not variables, as gettext doesn’t read variables and thus this won’t work properly

Viewing 1 replies (of 1 total)
  • The topic ‘How to save the 'checked' radio button state in database’ is closed to new replies.