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.
biffsmithAsked:
Who is Participating?
 
Michel PlungjanConnect With a Mentor IT ExpertCommented:
Not quite.

<form name="my_form" action="move_data.php" method="post"
onsubmit="return confirm('ARE YOU SURE YOU WANT TO MOVE DATA?')">
...
<input type="submit" value="Move that data!" />
...
</form>

or to keep the function


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

<form name="my_form" action="move_data.php" method="post" onsubmit="return confirmation();">
0
 
Robin HickmottSoftware DeveloperCommented:
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?
0
 
biffsmithAuthor Commented:
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.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
nsBlackOpsConnect With a Mentor Commented:
What is the URL of the action parameter for your form? Is it also pointing to "move_data.php?"

I don't like putting the function call in the onclick parameter of the submit button. I think it's cleaner to use the onsubmit property of the form tag. Then, if the user clicks CANCEL, you "return false;" and it stops the form from being submitted.

So...

<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 false;
      }
}
//-->
</script>

<form name="my_form" action="some_page.php" method="post" onsubmit="confirmation();">
...
<input type="submit" value="Move that data!" />
...
</form>
0
 
MardenvtConnect With a Mentor Commented:
Try removing the variable from the javascript.  All you are doing is checking if the variable is set or not, and in a true/false statement it will always be set. Hence your problem.  Try this instead, it works fine on my site:

<script type="text/javascript">
<!--
function confirmation() {
      if (confirm("ARE YOU SURE YOU WANT TO MOVE DATA?")) {
            window.location = "move_data.php";
      } else {
            return false;
      }
}
//-->
</script>
0
 
Michel PlungjanIT ExpertCommented:
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
0
 
biffsmithAuthor Commented:
Thanks to all of you!  It works for me using mplungjan and Mardenvt solutions!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.