• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5861
  • Last Modified:

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.
0
biffsmith
Asked:
biffsmith
3 Solutions
 
rhickmottCommented:
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
 
nsBlackOpsCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Michel PlungjanIT 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
 
MardenvtCommented:
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now