Link to home
Create AccountLog in
Avatar of biffsmith
biffsmith

asked on

JAVASCRIPT CONFIRM BOX & PHP

I have a form that, when submitted, moves data from one table to another in my mySQL db.  I am trying to implement a javascript CONFIRMATION box so the user must confirm before moving  the data.  

I have a small script written, but whether I click on OK or CANCEL, the data is still moved.

What am I doing wrong?  ANY HELP WOULD BE GREATLY APPRECIATED.
Avatar of Robin Hickmott
Robin Hickmott

PHP will only act on a request so assumingly a POST or GET is sent to the server everytime you click an option.

What is your Javascript code?
Avatar of biffsmith

ASKER

The Javascript is this:

<script type="text/javascript">
<!--
function confirmation() {
      var answer = confirm("ARE YOU SURE YOU WANT TO MOVE DATA?");
      if (answer){
            
            window.location = "move_data.php";
      }
      else{
            return;
      }
}
//-->
</script>

Then I have the form with onclick="confirmation()" in the submit button tag.

I get the confirmation box correctly - but no matter whether I click on OK or CANCEL my page is still redirected to move_data.php.

Thanks.
SOLUTION
Avatar of nsBlackOps
nsBlackOps

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
It is completely irrelevant if a variable is used or not.

The problem is most likely that the onSubmit does not have a return
Just calling a test in an onSubmit or an onClick does not do anything unless false is returned to the event handler when you want to cancel the event
Thanks to all of you!  It works for me using mplungjan and Mardenvt solutions!