简体   繁体   中英

terminating the php form action on click of confirmation box

We alert user using this javascript, now i want to add this functionality to my html form, which sends the data using the post method to my php form processing page.

Now what i want is to terminate the action if the user presses cancel button, it means all the data which has been sent using the post method should not be sent and the person would not be able to process the form until and unless he/she clicks the OK button. So is it possible? Or it is just some idiot question?

<html>
<head>
<script type="text/javascript">
function show_confirm()
{
var r=confirm("Press a button!");
if (r==true)
  {
  alert("You pressed OK!");
  }
else
  {
  alert("You pressed Cancel!");
  }
}
</script>
</head>
<body>

<input type="button" onclick="show_confirm()" value="Show a confirm box" />

</body>
</html>

First, you should put your ajax call only in the section of r===true Then you won't send any data to the server if the user is clicked on Cancel.

Second, in the case where you want to terminate an action (=ajax call) that the user already did (meaning, she click on the OK button and you called the ajax to post the data to your server) it will be harder because you will need to have a server side code that will do a 'rollback'. You can't 'catch' the data before it reach the server so your option here is to be able to do a rollback and then check the status from the client.

Call the confirm on form submit, not on button click, and change the button to a submit button:

<form onsubmit="return show_confirm();">
    <input type="text" name="test" value="test"><br>
    <input type="submit" value="Show a confirm box">
</form>

Add a return false; if cancel has been clicked:

function show_confirm() {
    var r=confirm("Press a button!");
    if (r===true) {
        alert("You pressed OK!");
    } else {
        alert("You pressed Cancel!");
        return false;
    }
}

Also see my example .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM