Reputation:
I have a javascript in my HTML form that runs when the user clicks the Submit button. It displays the total price for the users order and has the buttons for "ok" and "cancel". I want the form to be submitted when the user clicks "ok", but not submitted if the user clicks "cancel". It currently submits the form when either button is pressed. How do I keep it from submitting the form if "cancel" is clicked?
This is my short script:
<script type="text/javascript">
function calculate()
{
var gPrice
var aPrice
var sPrice
var total
gPrice = document.getElementById("grapeorderquantity").value * 4.0;
aPrice = document.getElementById("appleorderquantity").value * 3.0;
sPrice = document.getElementById("strbryorderquantity").value * 3.5;
total = (gPrice + aPrice + sPrice) * 1.08;
confirm("This is your total: " + total);
}
</script>
This is the HTML for my submit button:
<input type="submit" value="Submit" onClick="return calculate()" />
Upvotes: 1
Views: 1908
Reputation: 5235
Put this to your function:
if(confirm("This is your total: " + total)) { // confirm returns true = OK clicked
return true;
}
else { // confirm returns false = Cancel clicked
return false;
}
Upvotes: 1