2

I am working on a project , the situation is , the client has to enter the details , and after he enters the details , with java script ( confirm dialogue ) , i m showing all the important details for verification , then when he press ok, the form get submitted.

this is what i have made , but seems it is not working... need help ...

( after pressing cancel .. the dialog box keeps on coming .. and worst part is finally the form get submitted even if i have selected cancel )

( i have removed other details ...)

   <script type="text/javascript">
function validateMyForm()
{
    var X=confirm("ARE DETAILS CORRECT ??");
  if(!X)
  { 
    alert("validation failed false");
    returnToPreviousPage();
    return false;
  }
else
 {alert("validations passed");
  return true;}
}
</script>





<form name="frm" action=<?php print "new_update_attendence.php"; ?> method="POST"    
id="content" onsubmit="return validateMyForm();">
2
  • var x and X are not equivalent. Commented Aug 14, 2012 at 13:50
  • check for errors in returnToPreviousPage() what does that function do? Commented Aug 14, 2012 at 14:02

3 Answers 3

4

capitalization matters ALOT!

var x=confirm("ARE DETAILS CORRECT ??");
if(!X) // change to if(!x)

you could do, for second part of your question:

<script type="text/javascript">
    function validateMyForm()
    {
        var X=confirm("ARE DETAILS CORRECT ??");
        if(!X)
        { 
            setTimeout((function(){ window.location.reload(); }), 500);
            return false;
        }
        else
        {
            return true;
        }
    }
</script>
Sign up to request clarification or add additional context in comments.

6 Comments

changed ... but even after that .. the dialog box keep on coming even if i have selected cancel .
dialog validations passed will show if you hit OK, If you Hit Cancel will show validation failed false and call the returnToPreviousPage(); function.
what does returnToPreviousPage() do?
but the dialog box keeps on coming .. even if i press ok or cancel.
wat to change so that it comes only once .. if ok then form submitted else same page is loaded or retained..
|
3

X must be in lowerCase :

 if(!x)
  { ...

Comments

1

I gather you've used jQuery in the past. In jQuery return false would prevent the form from being submitted all together. That's because jQuery catches that return value (false) and translates it into eventObject.preventDefault(); eventObject.stopPropagation(); whereas return false, without jQuery only does the first (preventing the default action of that event). The event still bubbles/propagates.

change your HTML to onsubmit="return validateMyForm(event);"> to get access to the event object. Then, in JavaScript:

function validateMyForm(e)//e should hold the event
{
    e = e || window.event;//except for older IE versions(?), which we correct here
    //do whatever, if the form shouldn't be submitted:
    if (!confirm("ARE DETAILS CORRECT ??"))
    {
        if (e.preventDefault)
        {//chrome, FF, opera, ...
            e.preventDefault();
            e.stopPropagation();//<-- important line here
            return false;
        }//cruddy IE clients
        e.returnValue = false;
        e.cancelBubble = true;//<-- stop event in its tracks
        return false;
    }
}

That should do it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.