I have this php file which suppose to have a button to do some checkout functionally action by calling a JavaScript built-in function to do it
<a href="index.php" class="btn btn-danger btn-xs" > onclick="confirmAction()" <i class="icon-edit">Check out</a>
I used confirmAction() like this
<script>
// The function below will start the confirmation dialog
function confirmAction() {
let confirmAction = confirm("Dose the maintenance finished yet?");
if (confirmAction) {
alert("Action Done")
}
else {
alert("Action canceled");
}
}
</script>
if the user pressed ok I want to do php code which should do post action
controller.php?action=checkout&code=<?php echo $result->CONFIRMATIONCODE; ?>
but I wasn't successful in making it work inside the TRUE case in the javascript function
basically I tried to run the php code without the tag but I failed
any suggestion ? Thanks
Use code like below, if you want to redirect in cancel event then add code like: window.location.href = 'http://www.google.com';
Form:
<form method="POST" action="controller.php" id="myForm">
<input type="hidden" name="action" value="checkout">
<input type="hidden" name="code" value="put code here">
<a href="javascript:void(0)" class="btn btn-danger btn-xs" onclick="return confirmAction()" ><i class="icon-edit">Check out</i></a>
</form>
script:
function confirmAction() {
let confirmAction = confirm("Dose the maintenance finished yet?");
if (confirmAction) {
alert("Action Done");
document.getElementById("myForm").submit();
}
else {
alert("Action canceled");
return false;
}
}