1

Trying to make some database validation with Jquery Get method before submitting a form. But I get the error

Uncaught TypeError: form.submit is not a function

Got the logic form here

Simplified Code below (but the err is still there...)

<html>
<body>
    <div id="insertCertificateForm">
        <form action="/Certificates/Insert" method="post">
            <div>
                <label>Charge</label>
                <input name="Charge" id="Charge" />
            </div>
            <input type="submit" value="Insert" class="btn btn-default" />
        </form>
    </div>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
        $('#insertCertificateForm').submit(function (e) {
            e.preventDefault();
            var form = this;

            var Charge = $('#Charge').val();

            $.get("/Certificates/CheckIfChargeIsUnique", { Charge: Charge }, function (data) {
                if (data) {
                    form.submit();
                }
                else {
                    return false;
                }
            });
        });</script>
</body>
</html>
Andreas
  • 775
  • 2
  • 11
  • 20

3 Answers3

2

Because after clicking button this would mean the current button and

insertCertificateForm was never a form anyways...it was Div

best would be to bind the form with an ID #myForm

<html>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

    <body>
        <div id="insertCertificateForm">
            <form  id="Myform" action="/Certificates/Insert" method="post">
                <div>
                    <label>Charge</label>
                    <input name="Charge" id="Charge" />
                </div>
                <input type="submit" value="Insert" class="btn btn-default" />
            </form>
        </div>

        <script>
            $('#insertCertificateForm').submit(function (e) {
                e.preventDefault();
                var form = $("#Myform");

                var Charge = $('#Charge').val();

                $.get("/Certificates/CheckIfChargeIsUnique", { Charge: Charge }, function (data) {
                    if (data) {
                        form.submit();
                    } else {
                        return false;
                    }
                });
            });
        </script>
    </body>
</html>

and also load your scripts in the head

1stthomas
  • 731
  • 2
  • 15
  • 22
Rohit Kumar
  • 1,777
  • 2
  • 13
  • 26
0

Your selector is wrong $('#insertCertificateForm'), if you want to do like this you need to add this id into your form <form id="insertCertificateForm" otherwise follow this way,

$('form').submit(function (e) {
    e.preventDefault();

    var Charge = $('#Charge').val();

    $.get("/Certificates/CheckIfChargeIsUnique", { Charge: Charge }, function (data) {
        if (data) {
            $(this).submit();
        } else {
            return false;
        }
    });
});
1stthomas
  • 731
  • 2
  • 15
  • 22
Casper
  • 1,469
  • 10
  • 19
0

That's because you're calling this and not $(this) when declaring the form variable. You can either declare it as $(this) or use $(form) to submit the form.

Vig
  • 342
  • 1
  • 7