annoying "Confirm Form Resubmission" message

I'm trying to make a customized registration page in front end, but when I try to reload the page in error/success case it display that message in chrome:

Confirm Form Resubmission

The page that you're looking for used information that you entered. Returning to that page might cause any action you took to be repeated. Do you want to continue?

my code :

the_post();//Iterate the post index in the loop.    
get_header();
$err = '';
$success = '';

global $wpdb, $PasswordHash, $current_user, $user_ID;

if(isset($_POST['task'])  $_POST['task'] == 'register' ) {

    $pwd1 = (trim($_POST['pwd1']));
    $pwd2 = (trim($_POST['pwd2']));
    $first_name = (trim($_POST['first_name']));
    $last_name = (trim($_POST['last_name']));
    $email = (trim($_POST['email']));
    $username = (trim($_POST['username']));

    if( $email ==  || $pwd1 ==  || $pwd2 ==  || $username ==  || $first_name ==  || $last_name == ) {
        $err .= 'Please don\'t leave the required fields.br/';
    }
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $err .= 'Invalid email address.br/';
    }
    if(email_exists($email) ) {
        $err .= 'Email already exist.br/';
    }
    if(username_exists($username) ) {
        $err .= 'username already exist.br/';
    }
    if($pwd1  $pwd2 ){
        $err .= 'Password do not match.br/';
    } else {

        $user_id = wp_insert_user( array ('first_name' = apply_filters('pre_user_first_name', $first_name), 'last_name' = apply_filters('pre_user_last_name', $last_name), 'user_pass' = apply_filters('pre_user_user_pass', $pwd1), 'user_login' = apply_filters('pre_user_user_login', $username), 'user_email' = apply_filters('pre_user_user_email', $email), 'role' = 'subscriber' ) );
        if( is_wp_error($user_id) ) {
            $err = 'Error on user creation.';
        } else {
            do_action('user_register', $user_id);

            $success = 'You\'re successfully register';
        }

    }

}
    ?
div class=wrapper
    form method=post
        h3 class=center h1_headCreate Account./h3
        h6 class=center* Required Fields/h6
        !--display error/success message--
        div id=message
        ?php
            if(! empty($err) ) :
                echo 'p class=error'.$err.'';
            endif;  
            if(! empty($success) ) :
                echo 'p class=error'.$success.'';               
            endif;
        ?
        /div
        div class=row
            div class=
                input type=text value= name=first_name id=first_name class=validate/
                label* First Name/label
            /div
            div 
                input type=text value= name=last_name id=last_name class= validate/
                label* Last Name/label
            /div
        /div
        div
            input type=text value= name=email id=email class=icon_prefix validate /
            label* Email/label
        /div
            div 
                input type=text value= name=username id=username validate/
                label  * Username/label
            /div
            div 
                input type=text value= name=phone id=phone validate/
                label Phone/label
            /div

        /div
        div
            input type=password value= name=pwd1 id=pwd1 class=icon_prefix validate /
            label* Password/label
        /div
        div
            input type=password value= name=pwd2 id=pwd2 validate /
            label  * Password again/label
        /div

        /div
        div class=alignleftp?php if($success!= ) { echo $success; } ? ?php if($err != ) { echo $err; } ?/p/div
        button type=submit name=btnregister class=button btn right Register/button
        input type=hidden name=task value=register /
    /form
/div

?php get_footer() ?

My question is:

Why this message? How to solve this issue?

Topic login theme-development Wordpress

Category Web


This is more of general form issue than wordpress specific, but basically you should not submit forms to the page from which they are loaded at. You should have a dedicate URL which handles for submission and then redirects to wherever it is needed (success page on completion, back to the form if failed, etc). You can use the admin-post.php end point for that, which operates in a very similar way to the more familiar admin-ajax.php.

To use admin-post.php you need to specify it as the action url, so your form element will be something like <form method ="post" action="<?php echo esc_url(admin_url('admin-post.php'))?>" >

might be useful https://www.sitepoint.com/handling-post-requests-the-wordpress-way/

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.