简体   繁体   English

JavaScript setTimeout表单数据

[英]Javascript setTimeout form data

I have an html form: 我有一个HTML表单:

<form method="post" action="login-autentica" id="loginform" class="input-wrapper blue-gradient glossy" title="Login">
    <ul class="inputs black-input large">
        <li><span class="icon-user mid-margin-right"></span>
            <input type="text" name="porta" id="username" value="" class="input-unstyled" placeholder="Porta" autocomplete="off">
        </li>
        <li><span class="icon-lock mid-margin-right"></span>
            <input type="password" name="senha" id="password" value="" class="input-unstyled" placeholder="Senha" autocomplete="off">
        </li>
    </ul>
    <p class="button-height">
        <button type="submit" class="button glossy float-right" id="login">Entrar</button>
        <input type="checkbox" name="remind" id="remind" value="1" checked="checked" class="switch tiny mid-margin-right with-tooltip" title="Habilitar Auto-Login">
        <label for="remind">Lembrar</label>
    </p>
</form>

JavaScript is responsible for error messages and other things: JavaScript负责错误消息和其他事情:

$('#loginform').submit(function (event) {
    // Values
    var login = $.trim($('#username').val()),
        pass = $.trim($('#password').val());

    // Check inputs
    if (login.length === 0) {
        // Display message
        displayError('Por Favor, insira sua porta!');
        return false;
    } else if (pass.length === 0) {
        // Remove empty login message if displayed
        formWrapper.clearMessages('Por Favor, insira sua porta!');

        // Display message
        displayError('Por Favor, insira sua senha!');
        return false;
    } else {
        // Remove previous messages
        formWrapper.clearMessages();

        // Show progress
        displayLoading('Checando usuário CasCast...');

        // Stop normal behavior
        event.preventDefault();



        // Simulate server-side check
        setTimeout(function () {
            document.location.href = 'url'
        }, 2000);
    }
});

The problem is in the following statement: 问题在以下语句中:

"setTimeout (function () {document.location.href = 'url'}, 2000);"

My form html sends data to a php file called 'login-autentica.php' , The problem with this is that setTimeout can not send the data to PHP, returning the error. 我的表单html将数据发送到名为'login-autentica.php'的php文件,此问题是setTimeout无法将数据发送到PHP,并返回错误。

I want that when the User click 'submit' , data from HTML form to be captured and sent to the 'login-autentica.php' after the time-period mentioned in setTimeout() ,? 我希望当用户单击'submit' ,在setTimeout()提到的时间段之后捕获HTML表单中的数据并将其发送到“ login-autentica.php”中吗?

Thanks for helping me, excuse my english, I'm from Brazil... 感谢您的帮助,请问我的英语,我来自巴西...

You use document.location.href in setTimeout. 您可以在setTimeout中使用document.location.href。 Its an redirect (GET method), and ,of course, POST-data doesnt sending. 它是重定向(GET方法),并且,当然,POST数据不会发送。

Try to use in setTimeout 尝试在setTimeout中使用

        setTimeout(function() {
                $("#loginform").submit();
        }, 2000);

I have tested your code many times and got the reason. 我已经测试了您的代码很多次,并找到了原因。

The page will navigate to another page after the form submit. 提交表单后,该页面将导航到另一个页面。 Now if 2s (as per your code) passes before the navigation occurs then the js code that had to occur after 2s will not execute because a new page content is loaded in the browser. 现在,如果在导航发生之前2s (根据您的代码)过去了,那么2s之后必须发生的js代码将不会执行,因为浏览器中加载了新的页面内容。

Hence I tried reducing the timeout period and got the maximum allowed value 300-400. 因此,我尝试减少超时时间,并获得最大允许值300-400。 But that too isn't certain. 但这也不确定。 so you should keep it ideally ~0 ar at least as minimum as possible to possibly avert such a condition. 所以你应该把它最好~0 AR至少尽可能最小,以避免可能这样的条件。

If you're doing the login check, you should make an AJAX call which will store the user into the session of wherever you keep it if the credentials are correct, and then, if AJAX returns success, you should redirect to another page. 如果要进行登录检查,则应进行AJAX调用,如果凭据正确,它将把用户存储到保存用户的会话中,然后,如果AJAX返回成功,则应重定向到另一个页面。

The difference between the simulated server check (setTimeout) and the real one (AJAX call, for example) is that you can use a callback function in the latter, while you don't know what will happen in 2secs in the former. 模拟服务器检查(setTimeout)和真实服务器检查(例如AJAX调用)之间的区别在于,您可以在后者中使用回调函数,而您不知道前者在2秒内会发生什么。

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

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