Jump to content


Photo

Form Confirmation - Cancel Doesn't Cancel.


  • Please log in to reply
6 replies to this topic

#1 DaVuLf

DaVuLf
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 01 June 2006 - 03:14 PM

I have a form on my page.

<form action="insert.php" method="post">

I have a submit button on the page.
<input name="Submit" onclick="confirmation()" type="Submit" />

I have a confirmation function.
<script type="text/javascript">
<!--
function confirmation() {
    var answer = confirm("Is this transaction information correct?")
        if (answer){
            return true;
        }else{
            return false;
        }
}
//-->
</script>

Now I'm wondering why when I click submit, and it asks for confirmation, and I click cancel, it still submits the form. Shouldn't it just let me fill in more data? Or correct what was incorrect. The whole point of confirmation seems to suggest that if the data is incorrect, that we have the opportunity to modify it before submission. Unfortunately, it seems to me like that isn't happening in this instance.

Any help would be appreciated.

Thanks,
DaVuLf

#2 lead2gold

lead2gold
  • Members
  • PipPipPip
  • Advanced Member
  • 164 posts
  • LocationOttawa, On

Posted 01 June 2006 - 03:36 PM

[!--quoteo(post=379094:date=Jun 1 2006, 11:14 AM:name=DaVuLf)--][div class=\'quotetop\']QUOTE(DaVuLf @ Jun 1 2006, 11:14 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I have a form on my page.

<form action="insert.php" method="post">

I have a submit button on the page.
<input name="Submit" onclick="confirmation()" type="Submit" />

I have a confirmation function.
<script type="text/javascript">
<!--
function confirmation() {
    var answer = confirm("Is this transaction information correct?")
        if (answer){
            return true;
        }else{
            return false;
        }
}
//-->
</script>
[/quote]

i think you need to do this:

  <input name="Submit" onclick="return confirmation();" type="Submit" />


#3 DaVuLf

DaVuLf
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 01 June 2006 - 03:37 PM

That did the trick.

What is the difference?

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 01 June 2006 - 04:16 PM

the added keyword return before you call the confirmation function with in the onclick attribute on you input tag, Without that your form won't submit.

#5 DaVuLf

DaVuLf
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 01 June 2006 - 04:27 PM

I noticed what he had added, which changed the way it worked, but I was wondeirng why. In reality, the form itself was still submitting, and the actions were being processed. The issue was that the cancel portion was not working.

Regardless, it is working now, and for that I must thank Lead.

#6 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 01 June 2006 - 06:46 PM

[!--quoteo(post=379112:date=Jun 1 2006, 05:27 PM:name=DaVuLf)--][div class=\'quotetop\']QUOTE(DaVuLf @ Jun 1 2006, 05:27 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I noticed what he had added, which changed the way it worked, but I was wondeirng why. [/quote]

Because the onclick event must return the value 'false' in order to cancel the submission. Without explicitly returning the value from your confirmation function, it just gets lost and the default value is returned from the event, which is "true".

Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#7 DaVuLf

DaVuLf
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 01 June 2006 - 08:38 PM

Thanks Arthur, that's exactly what I was looking for.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users