onlyican Posted March 22, 2007 Share Posted March 22, 2007 Hey people I have a page for when the user has purchased something. The form has 2 submit buttons. One being "Cancel order" Other being "Continue To Buy" As you might guess, if the user Clicks "Cancel Order" I want to cancel the order. Now, what happens if the user Clicks "Canel order" By accident, they actually want to purchase. So I want a little bit of Javascript to check they want to cancel. Nothing to clever, something like this. This is what I done so far //Javascript Include File function ComfirmCancelOrder(){ var r=confirm("Are you sure you want to cancel this order?"); if(r==true){ return true; }else{ return false; } } function on the submit button for Cancel I have <input type='submit' name='submit' value='Cancel order' onclick='ConfirmCancelOrder();' /> So what happens now, is the user Clicks cancel, I get a pop up saying my text, and a OK and Cancel button. What I want is, If the user clicks OK, then the form is submitted, if the user Clicks Cancel, then nothing happens. But at the moment, whatever the user clicks the form is still submitting. How do I stop that form submitting if the user clicks cancel. Quote Link to comment https://forums.phpfreaks.com/topic/43804-solved-if-confirm-is-no-then-do-nothing/ Share on other sites More sharing options...
fenway Posted March 22, 2007 Share Posted March 22, 2007 First, you can just retun the confirm() call from the function, no need do have the extra code ... and you need to return the value of your function call to the onclick handler so that it aborts. Quote Link to comment https://forums.phpfreaks.com/topic/43804-solved-if-confirm-is-no-then-do-nothing/#findComment-212779 Share on other sites More sharing options...
onlyican Posted March 22, 2007 Author Share Posted March 22, 2007 Cheers for the advice, just returning the value But I am calling the function on the onclick handler, as mentioned above. <input type='submit' name='submit' value='Cancel order' onclick='ConfirmCancelOrder();' /> Or is this not what you meant. Quote Link to comment https://forums.phpfreaks.com/topic/43804-solved-if-confirm-is-no-then-do-nothing/#findComment-212838 Share on other sites More sharing options...
fenway Posted March 22, 2007 Share Posted March 22, 2007 Yes, but the handler doesn't know what to do with this value! Try: <input type="submit" name="submit" value="Cancel order" onclick="return ConfirmCancelOrder();" /> Much like you would for a "fake" href onclick or a form onsubmit validation. Quote Link to comment https://forums.phpfreaks.com/topic/43804-solved-if-confirm-is-no-then-do-nothing/#findComment-212890 Share on other sites More sharing options...
onlyican Posted March 23, 2007 Author Share Posted March 23, 2007 ahh excellent Normally for a fake href link, I do <a href='#' onclick='MyFunction(); return false' /> So I just need to return MyFunction(); Excellent work hombre. Quote Link to comment https://forums.phpfreaks.com/topic/43804-solved-if-confirm-is-no-then-do-nothing/#findComment-213352 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.