I am using php5.6 and printing html and trying to get a confirm propmt to work with JS. Ordering products from weppage it should ask if person is certain that the order is made and he/she is proceeding to checkout after "ok". Cancelled to continue shopping or change personal info.
Prompt works, if(!conf)
is executed and page is redirected to top, but it still submits the form and creates an order on cancel. Here is the code and that is included to other file if it matters. was searching stackoverflow for a while and didn't see this kind of a problem. Tried to put 'onclick' to button but it was even worse and got some errors.
ob_start();
?>
<script type="text/javascript">
function continueToPayment() {
var conf = confirm("<?php echo SQL::Translate('payment_confirm_text') ?>");
if(!conf) {
window.location.href = '#';
return false;
}
else{
return true;
}
}
</script>
<?php
print "<form onsubmit='return continueToPayment()' action='" . SQL::Table("Pages")->Href("thank_you") . "?pm=cash&ord=$next_reference_number' method=\"POST\">";
$amount = number_format((($_SESSION['sc_data']['final_total'])/100),2,".","");
print "<input type='hidden' name='action' value='send_cash_order'>";
echo ReturnOrderSubmitInfoHtml();
print "<button type='submit' id='send_order' class='pc-btn pc-btn-sc'>" . SQL::Translate(PrintPaymentText('cash')) . "</button>";
print "<br/>";
print "</form>";
$form = ob_get_clean();
In the form part it should be onsubmit='continueToPayment(event)'
.
Then in the function you need to stop default form behaviour by using event.preventDefault()
method.
function continueToPayment (event) {
var conf = confirm("<?php echo SQL::Translate('payment_confirm_text') ?>")
if (!conf) {
window.scrollTo(0, 0)
event.preventDefault()
return false
}
// else case unnecessary
}