Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Submit() not working

I have a from skeleton like below

@using (Html.BeginForm("Preapprove", "Preapproval", FormMethod.Post, new { name = "form1", id = "form1", enctype = "multipart/form-data", @class = "form-horizontal" }))
{
  ......
<input name="submit" type="button" value="Submit" id="Sub" class="btn btn-success" style="margin-left:950px" />                     


 }

And in jquery im submitting this form on the click even of the button id Sub

$(document).ready(function () {
            $('#Sub').click(function (e) {
              var error_flag = 0;

                if (d_row_count == 1) {
                    error_flag = 1;
                }
                else if (d_row_count > 1) {
                    if (code == null || code.trim() == '') {
                        error_flag = 1;

                    }
                    else if (s_row_count == 1) {
                        error_flag = 1;
                    }
                }
                if (error_flag != 1) {
                    alert("submitting");
                    $("#form1").submit();
                 }
            });

When all the validation passed the error_flag will be 0 and it is getting inside the if condition and giving me the alert "Submitting" but the form is not getting submitted.

Anything im missing on the code?

like image 761
Sachu Avatar asked Apr 26 '15 04:04

Sachu


1 Answers

You need to change the name of the input button from "submit" to something else. Having an element named "submit" causes the form's submit method to no longer work, which means the form cannot be submitted via JavaScript (at least, not easily).

The reason for this is because an input element named "submit" can be referenced by its name like this: form1.submit. That is, the input element is added as a property of the form object, with the property name being the name of the input element. That basically hides the submit function that is on the form's prototype.


I say "not easily" above, because the following still works (at least in Firefox):

HTMLFormElement.prototype.submit.call($("#form1")[0]);
like image 155
John S Avatar answered Nov 15 '22 07:11

John S