Reputation: 2181
On a Magento shopping cart, there are 2 forms that need to be filled out: shipping estimate and coupon code. Each refreshes the page after submitting the form. The shipping estimate form needs to be submitted before the coupon form so that the coupon can validate the country.
What I'd like to do is to call the shipping form's coShippingMethodForm.submit()
on load. That way the country is loaded, and then the coupon form will check the country. This has the added advantage that the initial shipping rates are displayed without requiring the user to submit the form.
My challenge: if I call coShippingMethodForm.submit()
on load - it will refresh the page and then continuously refresh b/c the coShippingMethodForm.submit()
keeps submitting and refreshing.
How do I call that function once, page refreshes, and not run again (unless the user clicks the submit button).
Example Magento cart page I'm referring to
Upvotes: 0
Views: 4055
Reputation: 5102
You can use cookie to prevent resumbit on reload.
Function js for broswer cookie can be found here http://www.quirksmode.org/js/cookies.html
In your JS function coShippingMethodForm.submit
coShippingMethodForm.submit = function(){
//check cookie here
if(readCookie('already_submit')==1)
return;
createCookie('already_submit', 1, 1); //for 1 day
//old code here
}
And on submit button, add eraseCookie line:
<input type="submit" id="your_submit_button"
onclick="eraseCookie('already_submit');" />
Upvotes: 1
Reputation: 21388
I see you're using PHP, why not just have a hidden form
<input type='hidden' name='countrySubmitted' value='true' />
and then when php renders the page check to see if the value is set, if not then call the function to submit.
echo "<script type='text/javascript'>";
if (isset($_POST['countrySubmitted']))
echo "document.coShippingMethodForm.Submit();";
echo "</script>";
Upvotes: 1