0

I'm trying to delay the submission of a form but only when i trigger the submit after the delay it does not send the post values.

this is my html

<form id="form_anim" name="form" autocomplete="off" action="index.php" method="POST">
    <input name="username" id="username" type="text" placeholder="Nome utente" autofocus required>
    <input name="password" id="password" type="password" placeholder="Password" required>
    <a href="#">Hai dimenticato la password?</a>
    <input id="invio" name="invio" type="submit" value="Accedi">
</form>

and this is the script

   $('#form_anim').on('submit', function (event, force) {
    if (!force) {
        var $this = $(this);
        event.preventDefault();
        setTimeout(function () {
            $this.trigger('submit', true);
        }, 2000);
    }
});

help me please.

11
  • 3
    why would you delay it may I ask? Commented May 28, 2015 at 15:05
  • 1
    Why Submitting form twice? Commented May 28, 2015 at 15:06
  • 2
    also, what is force ? you cannot possibly use it Commented May 28, 2015 at 15:06
  • 1
    It looks like you've already submitted the form... Commented May 28, 2015 at 15:06
  • 5
    You're using $this before you define it Commented May 28, 2015 at 15:07

4 Answers 4

2

This will effectively submit your form with a delay.

$('#form_anim').on('submit', function (e) {
    var form = this;
    setTimeout(function () {
        form.submit();
    }, 2000);
    return false;
});

(btw, you don't need jQuery for this, unless you want to support IE8 and below)

Demo page

Monitor your network (via browser inspector) to see it in action

Sign up to request clarification or add additional context in comments.

Comments

-1

note sure if you meant to submit twice?

$('#form_anim').on('submit', function(event, force) {
    if (!force) {
        var $this = $(this);
        setTimeout(function() {
            event.preventDefault();
            $this.trigger('submit', true);
        }, 2000);
    }
});

1 Comment

First prevent default, than do fancy stuff. Also giving an answer without knowing what is that force argument...
-1

Try following code It works smoothly

<form onsubmit="return delay('1')" id="form_anim" name="form" autocomplete="off" action="#" method="get">
    <input name="username" id="username" type="text" placeholder="Nome utente" autofocus required>
    <input name="password" id="password" type="password" placeholder="Password" required>
    <a href="#">Hai dimenticato la password?</a>
    <input id="invio" name="invio" type="submit" value="Accedi"/>
</form>

Javascript

<script>
    function delay(a){
        if(a == 1){
            setTimeout(delay(2),2000);
            return false;
        }
        else{
            $("#form_anim").submit();
        }
    }
</script>

Comments

-1

I recommend you to use a button without type="submit":

<button id="send">Send</button> 

$('#send').on('click', function(event) {                       
    setTimeout(function() {
       $('#form_anim').submit();
    }, 2000);        
});

10 Comments

yeah sorry i copied the wrong code; i wanted to copy the code you just posted, but it's not working; he send the submit but no post values
The problem is that you are canceling the submit each time
no, i checked with php and the submission is triggered but no values in $_POST. If i trigger the submit just before the timeout it works but obviously with no delay.If you have a solution please tell me,i'm trying to fix this for about 2 hours.
just tried.Not working, but now i know the problem is not the delay itself but the .trigger("submit").
$('#form_anim').trigger('submit', true); don't post values,even if not delayed. if i submit the form in the normal way with the input type="submit" post values are sent.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.