简体   繁体   中英

When submitting a hidden form to pass variables, how can we prevent the next form from resending the data?

I have a login form displayed always at the top right of my website. When an invalid username/password combination is found, a hidden form is generated based on the server response and submitted in order to redirect to a larger login (ie login.php)... When I submit my new attempt to login on the login.php login form after being redirected via the hidden form, the browsers warns "To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier." Wierdly enough, this message is only displayed on successful logins ( correct username/password as per database ).

Here is the Javascript function that handles the servers response when attempting to login

handleLoginXML: function(xml){
        var loginValue = xml.getElementsByTagName('info')[0].firstChild.nodeValue;
        if(loginValue === "success"){
        location.reload();
        }
        if(loginValue === "failed") {
        this.post('?page=login', { login_error : 'Invalid Email/Password'});
        }   
    },

Also, here is the post function that submits the hidden form on failed login

post: function(path, params, method='POST'){
        const form = document.createElement('form');
        form.method = method;
        form.action = path;
        for(const key in params) {
            if(params.hasOwnProperty(key)){
                const hiddenField = document.createElement('input');
                hiddenField.type = 'hidden';
                hiddenField.name = key;
                hiddenField.value = params[key];    
            form.appendChild(hiddenField);
            }
        }
        
        document.body.appendChild(form);
        form.submit();
    }

How can I prevent the form on?page=login from resending the hidden form data? Is it the way i'm dealing with successful logins that is the problem? Thanks

This is the problem right here location.reload(); , it is reloading the page like hitting the refresh button.

An easy alternative is: window.location.href = window.location.href; That should redirect the user back to the same URL without resubmitting of the form.

you can also reset the form: document.getElementById("myForm").reset();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM