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:
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:
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?
<p><input id="submitBtn" name="submitDetails" type="submit" value="Submit Details" onClick="return myClickHandler();" /></p>
<script type="text/javascript">
function myClickHandler(){
if(validation()){
showConfirm();
return true;
}
return false;
}
</script>
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");
}
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?
ASKER
@zappafan2k2, I have tried your code but still doin same thing
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");
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window
In other words, you want to return whatever value showConfirm() returns.