Forums

Run script but prevent automatic submit event

steveapple 06 Jan, 2012
Happy new year!
I have been using earlier versions of Joomla and various Chronoforms containing a javascript that validates and does pricing calculations. This is triggared by clicking a "calculate" button to run the script.

I have just upgraded to Joomla 1.7 and Cf 4 RC 3.0. Using the wizard a have added a button called "Calculate Price" as follows: <button onClick="DoQuotation(this.form.name)" style="color: red" ><b>Calculate total price</b></button>.

The javascript is:
function DoQuotation(formName)
{
var tradult;
var fareadult;
if ( document.forms[formName].NAdult.value == "")
{tradult = 0;
fareadult = 0;}
else
{tradult = parseInt(document.forms[formName].NAdult.value);
tradult = Number(tradult);
fareadult = tradult*175;}
.
.(snip)
.
farenett = faregross - farediscount;
pricetotal = farenett + priceoptions;
faregross = faregross.toFixed(2);
farediscount = farediscount.toFixed(2);
farenett = farenett.toFixed(2);
priceoptions = priceoptions.toFixed(2);
pricetotal = pricetotal.toFixed(2);
document.forms[formName].totalpass.value = totalpax;
document.forms[formName].payingpass.value = payingpax;
document.forms[formName].grossfare.value = "R " + faregross;
document.forms[formName].nettfare.value = "R " + farenett;
document.forms[formName].options.value = "R " + priceoptions;
document.forms[formName].total.value = "R " + pricetotal;
document.forms[formName].discount.value = "-R " + farediscount;
updatecookie(document.forms[formName]);
quoted = true;
return true;
}

---------------------------
Clicking the "Calclate Price" button correctly runs the javascript (from Firebug):
function onclick(event) {
DoQuotation(this.form.name);
}


However immediately after completing the script, it "submits" the form when it should not do so until the user is happy and has clicked the "submit" button. The form contents are emailed when they should not be. This causes a new blank page to be loaded (this will become a "thank you" page in due course).

Page URL:
http://friendsoftherail.com/joomla/index.php?option=com_chronoforms&chronoform=Booking_Cullinan

which gets automatically replaced by:
http://friendsoftherail.com/joomla/index.php?option=com_chronoforms&chronoform=Booking_Cullinan&event=submit

Question: How do I stop this unwanted automatic execution of the "submit" event after running the javascript?

Many thanks, Steve A
nml375 06 Jan, 2012
Hi Steve,
Try editing your javascript function to always return false instead of true.

/Fredrik
GreyHead 07 Jan, 2012
Hi steveapple ,

If this is happening in IE I think that - for some versions at least - the default event for an input with type='button; is 'submit'.

Bob
steveapple 09 Jan, 2012
Thanks, Fredrik. Tried that, but it still executes the submit after completion of the script. Clicking the browser back button re-displays the completed form but by then the unwanted submit has been actioned.
Thanks, Greyhead. I am using Firefox 9 but also tried IE 9 and the same thing happens.
steveapple 09 Jan, 2012
Also tried "return null;" but still the same problem.
nml375 09 Jan, 2012
Hi Steve & Bob,
It took a while to pin down, but it seems you've forgotten the type-property for your button element. If not defined, it will default to "submit" in most browsers, thus submitting the form.

Simply add a type='button' property, and things should work just fine:
<button onClick="DoQuotation(this.form.name)" style="color: red" type="button">
  <b>Calculate total price</b>
</button>


/Fredrik
steveapple 10 Jan, 2012
Hi Fredrik,
Thank you from the bottom of my heart!! It was indeed as simple as that! Fixed, now I can retore code I removed while debugging and complete the forms.
Once again, many thanks. Fredrik and Bob, look me up and I will buy you a beer or two if you get down here to Johannesburg one day.
This topic is locked and no more replies can be posted.