Link to home
Start Free TrialLog in
Avatar of carlbrooks1995
carlbrooks1995

asked on

Another submit button issue where page submits when user clicks Cancel

Hi below I have a submit button  and a jquery function it performs when the submit button is clicked on:

<p><input id="submitBtn" name="submitDetails" type="submit" value="Submit Details" onClick="return myClickHandler();" /></p>

Open in new window



         
<script type="text/javascript">
    			
function myClickHandler(){
     if(validation()){
                showConfirm();
                return true;
     }
     return false;
}

</script>

Open in new window


Now as you can see if the validation() function is met, then it will perform the showConfirm() function and this function will perform the confirmation box which is below:

    function showConfirm(){
    
         var confirmMsg=confirm("Make sure that your details are correct, once you proceed after this stage you would not be able to go back and change any details towards Questions, Options and Answers for your Assessment." + "\n" + "\n" + "Are you sure you want to Proceed?" + "\n" );
         
         if (confirmMsg==true)
         {
         submitform(); 
         return true;  
     }else{
     return false;
}
}
            
         function submitform()
{
    var fieldvalue = $("#QandA").val();
    $.post("insertQuestion.php", $("#QandA").serialize() ,function(data){
        var QandAO = document.getElementById("QandA");
        QandAO.submit();
    });  
    alert("Your Details for this Assessment has been submitted"); 
}

Open in new window


MY question is this. If the user clicks on OK in the confirmation box, then it submits the page which is fine. But if the user clicks on Cancel, then it should not submit the page. The problem is that it is submitting the page even though the Cancel button has been clicked on. Why is this?
Avatar of zappafan2k2
zappafan2k2

How about trying this:
<script type="text/javascript">
    			
function myClickHandler(){
     if(validation()){
                return showConfirm();
     }
     return false;
}

</script>

Open in new window

In other words, you want to return whatever value showConfirm() returns.
Avatar of carlbrooks1995

ASKER

@zappafan2k2, I have tried your code but still doin same thing
Avatar of leakim971
three ones :
function myClickHandler()
{
	var v = validation();
	if(v) c = showConfirm();
	return v && c;
}

function showConfirm()
{
	var a = confirm("Make sure that your details are correct, once you proceed after this stage you would not be able to go back and change any details towards Questions, Options and Answers for your Assessment." + "\n" + "\n" + "Are you sure you want to Proceed?\n");
	if(a) submitform();
	return a;
}
            
function submitform()
{
	var fieldvalue = $("#QandA").val();
	$.post("insertQuestion.php", $("#QandA").serialize() ,function(data){
		var QandAO = document.getElementById("QandA");
		QandAO.submit();
	});  
	alert("Your Details for this Assessment has been submitted"); 
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial