views:

257

answers:

2

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

A: 

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>";
Robert
+1  A: 
Bang Dao
Both answers here probably would work, but they made me realize a better way: check to see if the shipping rates div is present or not. If not, then submit coShippingMethodForm. Here's the JS is used: window.loaded=shippingLoaded(); function shippingLoaded() { if (checkobject("co-shipping-method-form")) { coShippingMethodForm.submit(); } } function checkobject(obj) { if (document.getElementById(obj)) { return false; } else { return true; } }
Joe Fletcher