0

It goes to the correct if statement, but either way it always submits the form. I dont want it to submit if the statement is true, and if they press cancel I also don't want it to submit.

I also saw lots of similar questions, but none of their answers were the correct solution here.

My form html:

<form action="actual link here" method="POST" target="_blank" onsubmit="ordering()">

My js:

function ordering() {
    if($('#total').html() == "$0.00") {
        alert("You have not ordered anything. Please use our contact page if you wish to contact us.");
        return false;
    }
    else {
        return window.confirm("Are you sure?");
    }
}
Tom Prats
  • 7,364
  • 9
  • 47
  • 77

3 Answers3

1

In order to stop the submit event, you need to specify return ordering() like:

<form action="actual link here" method="POST" target="_blank" onsubmit="return ordering()">
sevaor
  • 189
  • 2
0

You could change the button to the div and attach the function to it's onclick attribute:

<div id='submitButton' onclick="ordering()"></div>

And make a minor change to your function:

function ordering() {
   if($('#total').html() == "$0.00") {
     alert("You have not ordered anything. Please use our contact page if you wish to contact us.");
    }
    else if(window.confirm("Are you sure?")) {
    $('form').submit();
    }
}

If you don't mind writing a style for a div to make it look like a button.

TKharaishvili
  • 1,997
  • 1
  • 19
  • 30
0

As you're already using jQuery, you won't have to add an inline 'onsubmit=....' javascript on your page, just use this:

Give the form an ID (or a class) to make it easier to select it using jQuery;

<form id='myform' action="actual link here" method="POST" target="_blank">

<script>
$(document).ready(function () {

    $('#myform').on('submit', function(e) {
        if($('#total').html() == "$0.00") {
                e.preventDefault();
            alert("You have not ordered anything. Please use our contact page if you wish to contact us.");
            return false;
        } else {
            return window.confirm("Are you sure?");
        }
    }
});
</script>
thaJeztah
  • 27,738
  • 9
  • 73
  • 92