简体   繁体   English

在提交隐藏表单传递变量时,如何防止下一个表单重新发送数据?

[英]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."当发现无效的用户名/密码组合时,会根据服务器响应生成隐藏表单并提交以重定向到更大的登录名(即 login.php)...当我在登录名上提交新的登录尝试时.php 登录表单通过隐藏表单重定向后,浏览器警告“要显示此页面,Firefox 必须发送将重复之前执行的任何操作(例如搜索或订单确认)的信息。” 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这是在尝试登录时处理服务器响应的 Javascript function

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此外,这里是 function 的帖子,它在登录失败时提交隐藏表单

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?如何防止表单 on?page=login 重新发送隐藏的表单数据? Is it the way i'm dealing with successful logins that is the problem?是我处理成功登录的方式有问题吗? Thanks谢谢

This is the problem right here location.reload();这就是这里的问题location.reload(); , it is reloading the page like hitting the refresh button. ,它正在重新加载页面,就像点击刷新按钮一样。

An easy alternative is: window.location.href = window.location.href;一个简单的替代方法是: window.location.href = window.location.href; That should redirect the user back to the same URL without resubmitting of the form.这应该将用户重定向回相同的 URL 而无需重新提交表单。

you can also reset the form: document.getElementById("myForm").reset();您还可以重置表单: document.getElementById("myForm").reset();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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