2

I have a form that once the user clicks the Submit button - it runs 2 functions - (1) it pops up with a div that says 'Item added successfully to basket' then (2) a prefill function that sends the selected options to a Mals-E cart.

It works, but what i need to do is not let the form submit until the popup has shown for around 5 seconds - currently it flashes for a split second as the form is submitted immediately with the prefill function.

Can anyone help with this please?

Script Below:

function showDiv(f) {   
    document.getElementById('basket-center').style.display='block';
}   

function prefill(f) {

    var val = document.forms["basket"].elements["product[2]"].value;
    val = val + gettext(document.forms["form"].elements['users']);
    document.forms["basket"].elements["product[2]"].value = val;

    document.forms["basket"].elements["price"].value = document.forms["form"].elements['sum'].value;

    return false;
}

Form Script Here:

<form id="basket" name="basket" action="http://ww8.aitsafe.com/cf/add.cfm" method="post" onsubmit="showDiv(); prefill();">
    <input name="userid" type="hidden" value="A1251773"/>
    <input type="hidden" name="nocart"/>
    <input name="product[]" type="hidden" value="{b}Sage 50 Accounts (The VAT Edition) 2014{/b}"/>
    <input name="product[2]" type="hidden" value="{br}"/>
    <input name="product[3]" type="hidden" value="{br}FREE Installation onto 1 Server/PC & Same Day Download when ordered before 3pm"/>
    <input name="price" type="hidden" value=""/>
    <input name="noqty" type="hidden" value="1"/>
    <input name="thumb" type="hidden" value="sage50-thumb.png"/>
    <input name="return" type="hidden" value="http://10.10.0.195/reality-solutions-development/sage-50-accounts.php"/>
    <input type="image" src="img/buynow-button-green.jpg" border="0" alt="Buy Now" style="float:right; margin-top:-24px"/> 
</form>
Huangism
  • 15,324
  • 5
  • 45
  • 64
Daniel Palmer
  • 23
  • 1
  • 3

2 Answers2

5

Try this:

//html
<form id="my_form">
    <button id="my_button">submit</button>
</form>

//javascript
var my_form = document.getElementById("my_form"), button = document.getElementById("my_button");
my_form.onsubmit = function() {
    return false;
}

button.onclick = function() {
 setTimeout(function() {
    my_form.submit();
 }, 5000);
   return false;
}

Working jsfiddle: http://jsfiddle.net/njoqnwwf/

Christian Benseler
  • 7,216
  • 6
  • 33
  • 58
0

You could use Jquery for that

$(function() { $('#form').delay(milliseconds).submit(); });

codebased
  • 6,488
  • 7
  • 42
  • 78
  • this just submits for the form every 5 seconds - i want it to only submit once - after the user clicks the submit button it needs to wait 5 seconds then submit the form – Daniel Palmer Aug 13 '14 at 12:54